我有什麼,我認爲將是一個簡單的查詢爭奪......LINQ任何()無法正常運行
模式就像是:
Documents
(DocumentID,名稱)
Industries
(IndustryID,名稱)
Documents_Industries
(DocumentID,IndustryID)
數據是像:
DOCA - 增長的行業
DocB - 增長的行業,IndustryB
DOCC - 增長的行業,IndustryB
DocD - IndustryB
(所以上面將導致6行中Documents_Industries
數據,希望這是自-explanatory)
預期行爲:我試圖顯示一個由用戶選擇的行業篩選的文檔列表。如果選擇IndustryA,則結果集應爲DocA,DocB,DocC。如果選擇IndustryB,結果集應該是DocB,DocC,DocD。 如果選擇IndustryA和IndustryB,則結果集應爲DocB,DocC。
代碼到目前爲止:
IEnumerable<Document> docs = db.Documents.Where(l => l.IsActive == true);
// industryIdsSelected is an int[] from the user's selection
if (industryIdsSelected.Length > 0)
{
docs = docs.Where(l => l.Industries.Any(m => industryIdsSelected.Contains(m.IndustryID)));
}
實際行爲:如果選擇了增長的行業和IndustryB,結果集是DOCA,DocB,DOCC,DocD。 DocC,而不僅僅是DocB。我試過了.All()
,但那也行不通。我可能做錯了什麼?
所有將更接近,但它會更喜歡'L => SelectedIndustries.All(M => m.Contains(...' – BradleyDotNET
您正在使用多對多,如果選擇IndustryA和IndustryB爲什麼要排除文檔A和文檔B?它們都具有這些行業 – kat1330
@ kat1330,因爲這是要求:)用戶的篩選器比「或」 - 「獲取所有這兩個行業的所有文件」。 – user982119