2012-08-23 56 views
0

檢索相關數據,我有以下:LINQ無法從實體框架

db.Products.Where(p=>p.Sectors.Where(s=>s.website_id == websiteObj.website_id)).Count();

websiteObj不爲空,並且具有有效數據。產品和部門之間存在多對多的關係。此外,行業和網站之間還有一對多的關係。我沒有直接訪問網站上的產品,我不得不通過網站相關部門。

不管怎麼說,這個例子的工作原理: db.Sectors.Where(p=>p.website_id == websiteObj.website_id)).Count();

但問題是,第一個LINQ查詢給我下面的錯誤: Delegate 'System.Func<BusinessObjects.Product,int,bool>' does not take 1 arguments. Cannot convert lambda expression to type 'string' because it is not a delegate type.

和幾個其他錯誤。

無論如何,我做錯了什麼?這是我第一次嘗試LINQ。 在此先感謝。

+0

什麼結果,你想要得到的? –

回答

2

我認爲你需要Any在內部查詢:

db.Products.Where(p => p.Sectors.Any(s => s.website_id == websiteObj.website_id)) 
      .Count(); 

這將返回所有產品有至少一個部門具有指定website_id。

如果你希望所有的產品,其中所有行業屬於指定website_id,只需使用All代替Any

db.Products.Where(p => p.Sectors.All(s => s.website_id == websiteObj.website_id)) 
      .Count();