我正尝试在 c# 中为以下场景构建 LINQ 查询。
"Given a particular token "to identify the count of documents in which it exists"
private List<Document> documents = new List<Document>();
class Document
{
string filename;
public string FileName { get { return filename; } }
Dictionary<string, int> tokens;
public Dictionary<string, int> Tokens { get { return tokens; } }
public Document(string filename, Dictionary<string, int> tokens)
{
this.filename = filename;
this.tokens = tokens;
}
}
据我所知,我想到了这个表达式:
documents.Where(d => d.Tokens.Keys.Any(k => k == word)).Count();
但我不确定它是否 100% 正确。需要一些专家的建议。
请您参考如下方法:
要利用字典的 O(1) 查找时间,您应该改用 ContainsKey。
documents.Count(d => d.Tokens.ContainsKey(word));
