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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:18:02+00:00 2026-05-22T19:18:02+00:00

Let’s say I have this simple structure class FooDefinition { public FooDefinition Parent {

  • 0

Let’s say I have this simple structure

class FooDefinition
{
    public FooDefinition Parent { get; set; }
}

class Foo
{
    public FooDefinition Definition { get; set; }
}

class Bar
{
    public ICollection<Foo> Foos { get; set; }
}

A Bar has a list of Foos which can be simple (no parent/child relationships) or nested just one level (i.e. a parent Foo has many child Foos). As can be seen here, the relationships are specified in the FooDefinition, not the Foo itself.

What I need to do is generate a list of Foos properly grouped by this hierarchy. Consider the following source data:

var simpleDefinition = new FooDefinition();
var parentDefinition = new FooDefinition();
var childDefinition = new FooDefinition { Parent = parentDefinition };

var bar = new Bar { Foos = new[]
                           {
                               new Foo { Definition = simpleDefinition },
                               new Foo { Definition = parentDefinition },
                               new Foo { Definition = childDefinition }
                           }};

I’d like to get a collection of top-level items with their chilren. An adequate data structure would probably be IEnumerable<IGrouping<Foo, Foo>>.

The result would look like:

  • Item 1 (simple)
  • Item 2 (parent)
    • Item 3 (child)

And of course I’d like to do this with a purely-functional Linq query. I do lots of these, but my brain seems to be stuck today.

  • 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-22T19:18:03+00:00Added an answer on May 22, 2026 at 7:18 pm
    bar.Foos.Where(x => x.Definition.Parent == null)
            .Select(x => Tuple.Create(x, 
                                      bar.Foos.Where(c => c.Definition
                                                           .Parent == x.Definition
                                                    ))); 
    

    This will return an IEnumerable<Tuple<Foo, IEnumerable<Foo>>>, where Item2 of the Tuple contains the children for the parent in Item1. For your example, this returns two Tuples:

    • Item1 = simpleDefinition and Item2 containing an empty enumerable
    • Item1 = parentDefinition and Item2 containing an enumerable which contains childDefinition

    There might be a more elegant or faster way, but I couldn’t come up with it…

    Oh well, I contradict my own comment a little bit with this, but it is possible with GroupBy – at least nearly:

    bar.Foos.Where(x => x.Definition.Parent == null)
            .GroupBy(x => x,
                     x => bar.Foos.Where(c => c.Definition.Parent == x.Definition));
    

    This will return an IEnumerable<IGrouping<Foo, IEnumerable<Foo>>>.

    Update:
    I wanted to know, if the solution you wanted is possible at all.
    Yes, it is:

    bar.Foos.Where(x => x.Definition.Parent != null)
            .GroupBy(x => bar.Foos.Where(y => y.Definition == x.Definition.Parent)
                                  .Single(),
                     x => x)
            .Union(bar.Foos.Where(x => x.Definition.Parent == null && 
                                       !bar.Foos.Any(c => c.Definition.Parent == 
                                                          x.Definition))
                           .GroupBy(x => x, x => (Foo)null));
    

    But I really don’t want to know the big O of this and it really shouldn’t be used 😉

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

Sidebar

Related Questions

Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have an abstract parent class called shape, and that there are
Let's say that I have a set of relations that looks like this: relations
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I can call a method like this: core::get() . What is the
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say I have one class User, and it has a property of type
Let's say you have a class library project that has any number of supplemental

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.