Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:06:36+00:00 2026-05-30T15:06:36+00:00

I have a dictionary of struct, where one member is a list containing varying

  • 0

I have a dictionary of struct, where one member is a list containing varying elements applicable to each dictionary item.

I would like to join these elements against each item, in order to filter them and/or group them by element.

In SQL I’m familiar with joining against tables/queries to obtain multiple rows as desired, but I’m new to C#/Linq. Since a “column” can be an object/list already associated with the proper dictionary items, I wonder how I can use them to perform a join?

Here’s a sample of the structure:

name   elements
item1  list: elementA
item2  list: elementA, elementB

I would like a query that gives this output (count = 3)

name   elements
item1  elementA
item2  elementA
item2  elementB

For ultimately, grouping them like this:

   element    count
   ElementA   2
   ElementB   1

Here’s my code start to count dictionary items.

    public struct MyStruct
    {
        public string name;
        public List<string> elements;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MyStruct myStruct = new MyStruct();
        Dictionary<String, MyStruct> dict = new Dictionary<string, MyStruct>();

        // Populate 2 items
        myStruct.name = "item1";
        myStruct.elements = new List<string>();
        myStruct.elements.Add("elementA");
        dict.Add(myStruct.name, myStruct);

        myStruct.name = "item2";
        myStruct.elements = new List<string>();
        myStruct.elements.Add("elementA");
        myStruct.elements.Add("elementB");
        dict.Add(myStruct.name, myStruct);


        var q = from t in dict
                select t;

        MessageBox.Show(q.Count().ToString()); // Returns 2
    }

Edit: I don’t really need the output is a dictionary. I used it to store my data because it works well and prevents duplicates (I do have unique item.name which I store as the key). However, for the purpose of filtering/grouping, I guess it could be a list or array without issues. I can always do .ToDictionary where key = item.Name afterwards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:06:37+00:00Added an answer on May 30, 2026 at 3:06 pm
    var q = from t in dict
        from v in t.Value.elements
        select new { name = t.Key, element = v };
    

    The method here is Enumerable.SelectMany. Using extension method syntax:

    var q = dict.SelectMany(t => t.Value.elements.Select(v => new { name = t.Key, element = v }));
    

    EDIT

    Note that you could also use t.Value.name above, instead of t.Key, since these values are equal.

    So, what’s going on here?

    The query-comprehension syntax is probably easiest to understand; you can write an equivalent iterator block to see what’s going on. We can’t do that simply with an anonymous type, however, so we’ll declare a type to return:

    class NameElement
    {
        public string name { get; set; }
        public string element { get; set; }
    }
    IEnumerable<NameElement> GetResults(Dictionary<string, MyStruct> dict)
    {
        foreach (KeyValuePair<string, MyStruct> t in dict)
            foreach (string v in t.Value.elements)
                yield return new NameElement { name = t.Key, element = v };
    }
    

    How about the extension method syntax (or, what’s really going on here)?

    (This is inspired in part by Eric Lippert’s post at https://stackoverflow.com/a/2704795/385844; I had a much more complicated explanation, then I read that, and came up with this:)

    Let’s say we want to avoid declaring the NameElement type. We could use an anonymous type by passing in a function. We’d change the call from this:

    var q = GetResults(dict);
    

    to this:

    var q = GetResults(dict, (string1, string2) => new { name = string1, element = string2 });
    

    The lambda expression (string1, string2) => new { name = string1, element = string2 } represents a function that takes 2 strings — defined by the argument list (string1, string2) — and returns an instance of the anonymous type initialized with those strings — defined by the expression new { name = string1, element = string2 }.

    The corresponding implementation is this:

    IEnumerable<T> GetResults<T>(
        IEnumerable<KeyValuePair<string, MyStruct>> pairs,
        Func<string, string, T> resultSelector)
    {
        foreach (KeyValuePair<string, MyStruct> pair in pairs)
            foreach (string e in pair.Value.elements)
                yield return resultSelector.Invoke(t.Key, v);
    }
    

    Type inference allows us to call this function without specifying T by name. That’s handy, because (as far as we are aware as C# programmers), the type we’re using doesn’t have a name: it’s anonymous.

    Note that the variable t is now pair, to avoid confusion with the type parameter T, and v is now e, for “element”. We’ve also changed the type of the first parameter to one of its base types, IEnumerable<KeyValuePair<string, MyStruct>>. It’s wordier, but it makes the method more useful, and it will be helpful in the end. As the type is no longer a dictionary type, we’ve also changed the name of the parameter from dict to pairs.

    We could generalize this further. The second foreach has the effect of projecting a key-value pair to a sequence of type T. That whole effect could be encapsulated in a single function; the delegate type would be Func<KeyValuePair<string, MyStruct>, T>. The first step is to refactor the method so we have a single statement that converts the element pair into a sequence, using the Select method to invoke the resultSelector delegate:

    IEnumerable<T> GetResults<T>(
        IEnumerable<KeyValuePair<string, MyStruct>> pairs,
        Func<string, string, T> resultSelector)
    {
        foreach (KeyValuePair<string, MyStruct> pair in pairs)
            foreach (T result in pair.Value.elements.Select(e => resultSelector.Invoke(pair.Key, e))
                yield return result;
    }
    

    Now we can easily change the signature:

    IEnumerable<T> GetResults<T>(
        IEnumerable<KeyValuePair<string, MyStruct>> pairs,
        Func<KeyValuePair<string, MyStruct>, IEnumerable<T>> resultSelector)
    {
        foreach (KeyValuePair<string, MyStruct> pair in pairs)
            foreach (T result in resultSelector.Invoke(pair))
                yield return result;
    }
    

    The call site now looks like this; notice how the lambda expression now incorporates the logic that we removed from the method body when we changed its signature:

    var q = GetResults(dict, pair => pair.Value.elements.Select(e => new { name = pair.Key, element = e }));
    

    To make the method more useful (and its implementation less verbose), let’s replace the type KeyValuePair<string, MyStruct> with a type parameter, TSource. We’ll change some other names at the same time:

    T     -> TResult
    pairs -> sourceSequence
    pair  -> sourceElement
    

    And, just for kicks, we’ll make it an extension method:

    static IEnumerable<TResult> GetResults<TSource, TResult>(
        this IEnumerable<TSource> sourceSequence,
        Func<TSource, IEnumerable<TResult>> resultSelector)
    {
        foreach (TSource sourceElement in sourceSequence)
            foreach (T result in resultSelector.Invoke(pair))
                yield return result;
    }
    

    And there you have it: SelectMany! Well, the function still has the wrong name, and the actual implementation includes validation that the source sequence and the selector function are non-null, but that’s the core logic.

    From MSDN: SelectMany “projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a generic linked list struct like this template <typename E, typename F>
I have a dictionary that goes like this: typedef struct dictNode { int key;
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
Does VBA have dictionary structure? Like key<>value array?
Long story short, I have a struct (see below) that contains exactly one field:
I have a struct struct myStruct { Dictionary<string, int> a; Dictionary<string, string> b; ......
I have a Dictionary<string, XMLMessage> where XMLMessage is a struct: private struct XMLMessage {
I have a Dictionary and a List of keys to remove from the dictionary.
I have a huge dictionary of blank values in a variable called current like
I have a Dictionary where on of the members of DetailedObject is a List.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.