c# - 表达式<Func<PartJoinTable, bool>> predicate=null;在 where condition(query.Where(predicate)) 中抛出错误

标签 c# linq

 Expression<Func<PartJoinTable, bool>> predicate = null;

 var query2 = query.Join(partJoinTableRepository.GetPartJoinQuery(),
                         "x.PartID", "PartID", "inner" + row + "", null);
 foreach (var item1 in query)
 {
     list.Add(new PartJoinTable
     {
          ProductTypeId = item1.x.ProductTypeId,
          PartId = item1.x.PartId
     });

  }
  predicate = PredicateBuilder.True(list.AsQueryable());


  if (typeid=="3")
  {
      if (row == 0)
      {
          predicate1 = predicate1.And(z => cpaclassids.Contains(z.x.ProductTypeId.ToString()));
      }
      else
      {
          predicate = predicate.And(x => cpaclassids.Contains(x.ProductTypeId.ToString()));
       }
    }
    row++;
}
int lst1 = query.Where(predicate).Select(x => x).Distinct().ToList().Count();

Cannot convert from 'System.Linq.Expressions.Expression>' to 'System.Func<, bool>'

我无法检索结果,因为当我声明变量 Expression<Func<PartJoinTable, bool>> predicate = null; 时显示上述错误

最佳答案

回答

Expression> predicate=null; throws error in where condition(query.Where(predicate))

if (predicate != null)
    query = query.Where(predicate);

int lst1 = query.Distinct().Count();

仅在 where 子句存在时添加它。在 .Count() 之前使用 .ToList() 可以将数据库中的每一行投影到内存中。

关于c# - 表达式<Func<PartJoinTable, bool>> predicate=null;在 where condition(query.Where(predicate)) 中抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51856730/

相关文章:

c# - ASP.Net MVC TryUpdateModel() 等效于在 Windows 应用程序中使用

c# - 如何将 "var"用法(而不是显式类型)视为警告?

c# - 从 Lucene 查询中获取不同值的更快方法

c# - 使用 SMO 或 Linq 的 SQL Server CLR 触发器?

c# - 低效的 Entity Framework 查询

c# - 我有 .H 文件,如何将其公共(public)方法打开到 .Net 库?

c# - 为什么我不能在我自己的类中访问 UmbracoHelper

c# - 客户端查询生成器 (jquery) 到 Entity Framework 查询

c# - 如何比较相同的属性值,而不管多个模型中的顺序或重复

c# - 避免在 LINQ 查询中双重控制搜索