开发者

LINQ query for Dictionary items in C#

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜