2012-05-29 83 views
1

我使用PredicateBuilder生成where子句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

眼前的問題轉換似乎是dbCompany.dbCompanyLocationsEntitySet,它實現IEnumerable<T>而非IQueryable<T>。這意味着它的Where擴展方法需要Func<dbCompanyLocation, bool>,但是您提供的locationFilter變量是Expression<Func<dbCompanyLocation, bool>>

您可以通過調用Compile方法從locationFilter創建Func<dbCompanyLocation, bool>

但是,另一個問題是,即使它進行了類型檢查,c => c.dbCompanyLocations.Where(locationFilter)也不是謂詞,因爲Where返回的是IEnumerable<T>而不是bool。您可能打算使用Any而不是Where,即

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