通过在entityframework中动态传递要过滤的列,在列上选择不同的值

时间:2018-04-25 11:51:57

标签: c# entity-framework linq entity-framework-6

从条件= 2000

的Sometable中选择不同的描述

我尝试使用反射实现此功能,但收到错误

500 Internal Server Error","error":"{\"message\":\"An error has occurred.\",\"exceptionMessage\":\"LINQ to Entities does not recognize the method 'System.Reflection.PropertyInfo[] GetProperties()' method, and this method cannot be translated into a store expression.

我用linq查询编写的方法

   public IList<Material> GetDisctinctColumnValues(string parameter)
           {
                using (var db = this.dbFactory.CreateDbContext())
                {

                    var columns= db.Table.GroupBy(m => ((IQueryable)m).ElementType.GetProperties().Where(p => p.Name == parameter))
                                  .Select(c => c.FirstOrDefault()).ToList();
                    return columns;
                }
            }

我必须根据从方法传递的参数编写一个linq查询。假设Column作为参数传递,这里是“描述”。

1 个答案:

答案 0 :(得分:0)

var res = Table
  .Where(r => r.discreption==2000 })
  .GroupBy(x => x.SomeProperty)
  .Select(x => x.First());

  var res = Table
      .Where(r => r.discreption==2000 })
      .Distinct().ToList();

如果您只想select说明:

var res = Table.Select(z=>z.description)
  .Where(r => r.description==2000 })
  .GroupBy(x => x.SomeProperty)
  .Select(x => x.First());

  var res = Table.Select(z=>z.description)
      .Where(r => r.description==2000 })
      .Distinct().ToList();