简体   繁体   English

LINQ查询C#中的Dictionary项

[英]LINQ query for Dictionary items in C#

I have a Dictionary where on of the members of DetailedObject is a List. 我有一个字典,其中的DetailedObject成员是一个列表。 I am trying to write a query against the Dictionary to return all of the items where the DetailedObject has a specific value for one of the fields in it's SubStructure. 我正在尝试针对字典编写查询,以返回所有详细项目,其中在其SubStructure的某个字段中,DetailedObject具有特定值。 For example: 例如:

public struct SubStructure
{
  public int Id;
  public string SubSpecificFile;
}

public class DetailedObject  
{
  public int Id;
  public List<SubStructure> subs = new List<SubStructure>();
}

public Dictionary<int, DetailedObject> dict = new Dictionary<string, DetailedObject>();

Each SubStructure can appear inside zero or more DetailedObject instances. 每个SubStructure可以出现在零个或多个DetailedObject实例内。

I would, for example, like to query "dict" for each DetailedObject whose "subs" collection contains a SubStructure item with the Id of 3. 例如,我想为每个“ subs”集合包含ID为3的SubStructure项的DetailedObject查询“ dict”。

dict.Values.Where(d => d.subs.Any(ss => ss.Id == 3))
var result = dict1.Where(kvp => kvp.Value.subs.Any(ss => ss.Id == 3));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM