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 6798973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:47:36+00:00 2026-05-26T18:47:36+00:00

I have a normal GroupBy operation on an enumerable: e.GroupBy(i => i.Property) But if

  • 0

I have a normal GroupBy operation on an enumerable:

e.GroupBy(i => i.Property)

But if i.Property is really a collection, how would I break apart the collection and use the list’s elements as grouping keys?

For example let’s say I have two objects (Z, Y) that each have a list:

Z: { List = { A, B, C }}
Y: { List = { B, C, D }}

Now running the GroupBySubelement(o => o.List) would not group by the list itself, but would iterate over the list and generate the following Groupings.

{A, {Z}}
{B, {Z, Y}}
{C, {Z, Y}}
{D, {Y}

Is this possible?

Thanks!

  • 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-26T18:47:37+00:00Added an answer on May 26, 2026 at 6:47 pm

    Here’s some example code that achieves what you want:

    //This is just temporary data.  Has the similar structure to what you want.
    var parts = new[]
                    {
                        new
                            {
                                Name = "X",
                                Property = new[] {'A', 'B', 'C'}
                            },
                        new
                            {
                                Name = "Y",
                                Property = new[] {'B', 'C', 'D'}
                            },
                        new
                            {
                                Name = "Z",
                                Property = new char[] { }
                            }
                        };
    
    var groupedBySub = from part in parts
                       from sub in part.Property
                       group part by sub;
    
    foreach(var group in groupedBySub)
    {
        Console.WriteLine("{0} - {1}", group.Key, string.Join(", ", group.Select(x => x.Name)));
    }
    

    Which outputs:

    A - X
    B - X, Y
    C - X, Y
    D - Y
    

    You can also achieve this in the method chain fashion:

    var groupedBySub = parts.SelectMany(part => part.Property, (part, sub) => new {part, sub}).GroupBy(t => t.sub,  t => t.part);
    

    If you want to capture it with the list being empty:

    var groupedBySub = from part in parts
                       from sub in part.Property.DefaultIfEmpty()
                       group part by sub;
    

    Which when substituted for the code above, gives output:

    A - X
    B - X, Y
    C - X, Y
    D - Y
      - Z
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have normal sqlite database and want to use fts3. As mentioned in http://www.sqlite.org/fts3.html
I have turned a normal PHP class into a library so I can use
I have a normal database and not using any membership provider and I would
I have a list of 'request' objects, each of which has fairly normal activerecord
Suppose I have normal button.Click event. If I try to access UI elements from
I have normal WCF service with few methods. I would like to log some
I have a normal NavigationController. When i open one modalView (using segues) and dismiss
I have a normal textbox in my application. I can paste data to it
I have a normal asp.net page containing some code that I want to measure
I have a normal UINavigationController with a UIViewController contained in it, when my view

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.