2014-10-22 58 views
0

這裏的建立和組織行給出了它們不能將IEnumerable隱式轉換爲bool的錯誤。我錯過了什麼?在方法結束之前,我不能使用貪婪的操作符。我相信這很簡單,但我看不到它。動態LINQ2Entities查詢無法將IEnumerable轉換爲bool

IQueryable<User> query = context.Users; 

if (usvm.EstablishmentId > 0) 
    query = query.Where(x => x.UserEstablishments 
     .Where(y => y.UserEstablishmentId == usvm.EstablishmentId)); 

if (usvm.OrganisationId > 0) 
    query = query.Where(x => x.UserEstablishments 
     .Where(y => y.Establishment.OrganisationId == usvm.OrganisationId)); 

if (!String.IsNullOrEmpty(usvm.Forename)) 
    query = query.Where(x => x.Forename == usvm.Forename); 

if (!String.IsNullOrEmpty(usvm.Surname)) 
    query = query.Where(x => x.Surname == usvm.Surname); 

if (usvm.DOB != null) 
    query = query.Where(x => x.DOB == usvm.DOB); 

List<User> m = query.ToList(); 
return ToViewModel(m); 
+0

你在哪裏設置usvm? – Rugdr 2014-10-22 11:35:44

+0

這是從控制器傳遞的視圖模型。價值在那裏,所以這不是問題。它正在獲取我無法完成的查詢。 – Lee 2014-10-22 11:37:04

回答

3

你可以使用Any方法:

if (usvm.EstablishmentId > 0) 
    query = query.Where(x => x.UserEstablishments 
     .Any(y => y.UserEstablishmentId == usvm.EstablishmentId)); 

if (usvm.OrganisationId > 0) 
    query = query.Where(x => x.UserEstablishments 
     .Any(y => y.Establishment.OrganisationId == usvm.OrganisationId)); 
+0

不知道爲什麼這是倒投了,我會試試看。 – Lee 2014-10-22 11:35:03

+0

兩者都應該使用任何 – Rugdr 2014-10-22 11:40:09

相關問題