PredicateBuilder where子句问题

时间:2012-05-29 23:17:18

标签: predicatebuilder

我使用PredicateBuilder生成where子句

var locationFilter = PredicateBuilder.True<dbCompanyLocation>();
locationFilter = locationFilter.And(s => s.IsPrimary == true && s.State == practiceState);

var companyPredicate = PredicateBuilder.True<dbCompany>();
companyPredicate = companyPredicate.And(c => c.dbCompanyLocations.Where(locationFilter));

我收到以下错误,任何人都可以为此提供帮助,或者我做错了什么。

实例参数:无法从'System.Data.Linq.EntitySet'转换为'System.Linq.IQueryable'

1 个答案:

答案 0 :(得分:1)

直接问题似乎是dbCompany.dbCompanyLocationsEntitySet,它实现了IEnumerable<T>而不是IQueryable<T>。这意味着其Where扩展方法需要Func<dbCompanyLocation, bool>,但您提供的locationFilter变量为Expression<Func<dbCompanyLocation, bool>>

您可以通过调用Func<dbCompanyLocation, bool>方法从locationFilter创建Compile

然而另一个问题是即使它进行了类型检查,c => c.dbCompanyLocations.Where(locationFilter)也不是谓词,因为Where返回IEnumerable<T>而不是bool。您可能打算使用Any而不是Where,即

companyPredicate = companyPredicate.And(c => c.dbCompanyLocations.Any(locationFilter.Compile()));