0

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 SubStructure 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.

2
  • 4
    Any good reason why you're using a struct for SubStructure? Commented Aug 1, 2011 at 11:36
  • Using a struct because it is only going to contain data. No additional processing required. Commented Aug 1, 2011 at 13:51

2 Answers 2

7
dict.Values.Where(d => d.subs.Any(ss => ss.Id == 3))
Sign up to request clarification or add additional context in comments.

2 Comments

This way you miss the Key of each dictionary entry.
Quoting OP: 'query "dict" for each DetailedObject'... no requirement to get the key.
6
var result = dict1.Where(kvp => kvp.Value.subs.Any(ss => ss.Id == 3));

2 Comments

If you add ToDictionary(x => x.Key, x => x.Value) to the end you end up with a dictionary again.
what if I want to get a collection (e.g. .ToList()) of the 'SubSpecificFile' variables related with Id ==3 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.