[英]LINQ: Searching inside child entities
我有“屬性”對象和“ BayOption”子對象。 我需要站點搜索來搜索子BayOptions以及“屬性”,如果搜索條件與子或父項匹配,則返回“屬性”。 一個屬性可以具有多個BayOptions(通常如此)。 我不確定.Select或.SelectMany是否是我需要的技巧。 到目前為止,我有以下內容:
var stringResults = db.Properties
.Where(x => x.Address.Contains(id)... (more conditions here...but then BayOptions)
|| x.BayOptions.Select(g => g.Description).Contains(id)
);
但是.Select這里只會選擇一個BayOption嗎? (我不這么認為,但是.SelectMany讓我感到奇怪...)無論如何,我並沒有獲得像這樣的子對象的結果。
一種選擇是使用LINQ .Any()
:
var stringResults = db.Properties.Where(x =>
x.Address.Contains(id) ||
/* Other conditions || */
x.BayOptions.Any(g => g.Description.Contains(id)));
在這里,如果任何BayOptions
值具有包含ID的描述,則x.BayOptions.Any(g => g.Description.Contains(id))
將返回true。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.