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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:12:10+00:00 2026-05-29T19:12:10+00:00

class Foo() { public Bar [] bars; } class Bar () { public string

  • 0
class Foo()
{
    public Bar [] bars;
}

class Bar ()
{
    public string name;
    public int id;
}

I have an IEnumerable<Foo> and I want to construct an IEnumerable<Bar> which contains which contains every unique bar (and ordered by ID…but I can handle that part on my own).

I feel that this is the type of task that LINQ is made for..but I can’t seem to figure out how to make it happen.

As an example, if I had:

foo[0] => bars { { name = "one", id = 1 }, { name = "two", id = 2 }, { name = "three", id = 3 },
foo[1] => bars { { name = "four", id = 4 }, { name = "four", id = 4 }

I would want my linq statement to return:

{ name = "one", id = 1 }, { name = "two", id = 2 }, { name = "three", id = 3 }, { name = "four", id = 4 }
  • 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-29T19:12:12+00:00Added an answer on May 29, 2026 at 7:12 pm

    If your Bar class overrode Equals and GetHashCode, it would be very simple:

    var values = foo.SelectMany(f => f.bars)
                    .Distinct()
                    .OrderBy(b => b.id);
    

    Without that equality implementation, you’ll need to create your own IEqualityComparer<Foo> to pass into the Distinct method.

    If you only need to care about the id to make them distinct, you could use DistinctBy from MoreLINQ to make your life simpler:

    var values = foo.SelectMany(f => f.bars)
                    .DistinctBy(b => b.id)
                    .OrderBy(b => b.id);
    

    Or you could use an anonymous type as a quick hack to get an equality comparer across both values, still with MoreLINQ:

    var values = foo.SelectMany(f => f.bars)
                    .DistinctBy(b => new { b.id, b.name })
                    .OrderBy(b => b.id);
    

    It would almost certainly be better to implement equality in Bar though 🙂

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

Sidebar

Related Questions

Let's say I have a class: class Foo { public string Bar { get
Suppose you have a collection of Foo classes: class Foo { public string Bar;
I have something like this: public class Foo { public Bar[] Bars{get; set;} }
I have the classes Foo and Bar : public class Foo { public int
class Foo { public: void bar(); }; void Foo::bar() { static int n =
If I have the following code: public class Foo { public void Bar() {
I have the following piece of code: class Foo { public Foo() { Bar
Suppose I have: class Foo { ... }; class Bar : public Foo {
Concerning data binding I have these classes: public class Foo : List<Bar> { public
Suppose I have a class public class Foo { public Bar Bar { get;

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.