簡體   English   中英

LINQ無法從實體框架中檢索相關數據

[英]LINQ Unable to retrieve related data from entity framework

我有以下內容:

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

websiteObj不為null且具有有效數據。 產品和行業之間存在多種關系。 此外,部門和網站之間存在一對多的關系。 我沒有從網站直接訪問產品,我必須通過網站相關部門。

無論如何,這個例子有效: 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。 提前致謝。

我認為你在內部查詢中需要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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM