我是LINQ的新手
我正在尝试使用延迟加载执行动态 where
,但我不明白该如何做。
这是我的代码
protected int AnimalCount(System.Func<Animal, bool> expression = null, System.Func<Animal, bool> additionalExpression= null)
{
var records = (from temp in DbContext.Animal select temp);
if (expression != null) records = records.Where(expression).AsQueryable();
if (additionalExpression != null) records = records.Where(additionalExpression).AsQueryable();
return records.Count();
}
现在的问题是查询非常缓慢,我认为这是因为where子句被应用于查询的SELECT * FROM Animal
列表上。
protected int AnimalCount(params System.Func<Animal, bool>[] expressions)
?然后您可以使用循环来应用它们。 - IgorAsQueryable()
在那里是无用的。 - Mat J