[英]Finding parent collection by filtering collection of complex type using LINQ
我有以下課程結構
public class Document
{
[BsonId]
public MongoDB.Bson.ObjectId _id { get; set; }
public MongoDB.Bson.ObjectId Id
{
get;
set;
}
private List<KeywordModel> _domain;
public string DocumentName { get; set; }
public int FileSize { get; set; }
public string DocumentType { get; set; }
public string ContentType { get; set; }
public string DateTime { get; set; }
public List<KeywordModel> Domain {
get { return this._domain; }
set { this._domain = value;}
}
public Document()
{
this._domain = new List<KeywordModel>();
}
}
public class KeywordModel
{
public string Keyword { get; set; }
public int Frequency { get; set; }
}
我有一個父Document類的集合,例如: List<Document>
。 我想編寫一個LINQ查詢,該查詢將使用List<string>
並與KeywordModel.Keyword
的屬性匹配。 此外,它從List<Document>
返回Document
的集合。
具有至少1個關鍵字的所有文檔均匹配:
List<string> keywords = ...;
IEnumerable<Document> allDocuments = null;
var documents = allDocuments.Where(d => d.Domain.Any(kw => keywords.Contains(kw.Keyword))).ToList();
非Linq方法:
您可以使用List<T>
的FindAll-Methods。 例如:
List<string> matches = new List<string> {"test", "test2", "test3"};
documentList.FindAll(d => d.Domain.Exists(keyModel => matches.Contains(keyModel.KeyWord)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.