开发者

LINQ query for Dictionary items in C#

开发者 https://www.devze.com 2023-03-25 14:01 出处:网络
I have a Dictionary where on of the members of DetailedObject is a List. I am trying to write a query against the Dictionary to return all of the items where the DetailedObject has a specific value fo

I have a Dictionary where on of the members of DetailedObject is a List. 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. 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 S开发者_开发问答ubStructure can appear inside zero or more DetailedObject instances.

I would, for example, like to query "dict" for each DetailedObject whose "subs" collection contains a SubStructure item with the Id of 3.


dict.Values.Where(d => d.subs.Any(ss => ss.Id == 3))


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号