Dynamic column name in where clause. Entity Framework

后端 未结 4 1792
遥遥无期
遥遥无期 2021-01-23 01:06

I want to dynamically place column name in my select-query. How I can do this?

public IEnumerable MyTable MySelect(string colName, string param)
{
  using (MyEnt         


        
4条回答
  •  后悔当初
    2021-01-23 01:46

    Using dynamic linq:

    using System.Linq.Dynamic;
    
    public IEnumerable MyTable MySelect(string colName, string param)
    {
      using (MyEntities db = new MyEntities ())
      {
         var query = db.MyTable.Where($"{colName} LIKE %{param}%");
         return query;
      }
    }
    

提交回复
热议问题