小编Cho*_*tor的帖子

使用Linq to Entities(EF6)动态选择列名

我想在使用EF6时封装常见的场景.这是一个例子:

   public class StringRequest : DbRequestProperty
   {
      public string Name { get; set; }
      public bool? ExactMatch { get; set; }

      protected override bool IsValid()
      {
         return !string.IsNullOrWhiteSpace(Name);
      }

      private bool RequestExactMatch()
      {
         return ExactMatch.HasValue && ExactMatch.Value;
      }

      protected override IQueryable<T> Execute<T>(IQueryable<T> original, string propertyName)
      {
         return RequestExactMatch()
            ? original.Where(o => GetProperty<string>(o, propertyName) == Name)
            : original.Where(o => GetProperty<string>(o, propertyName).Contains(Name));
      }
   }
Run Code Online (Sandbox Code Playgroud)

但GetProperty无法转换为查询.所以我正在考虑使用"propertyName"动态选择列.

  protected override IQueryable<T> Execute<T>(IQueryable<T> original, string propertyName)
  {
     return RequestExactMatch()
        ? original.Where(o => GetColumnByName<string>(propertyName) == Name)
        : …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-entities entity-framework entity-framework-6

3
推荐指数
1
解决办法
2039
查看次数