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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:10:14+00:00 2026-06-16T02:10:14+00:00

Sorry about the question, I couldn’t build the sentence. Here is what I have,

  • 0

Sorry about the question, I couldn’t build the sentence. Here is what I have,

class Brand{
    int ModelId;
    string name;
}

class Gallery{

    IList<Brand> brands;
    ...
    public BrandList{
        get{ return brands; }
    }
}

I have a list of Gallery. Like this,

IList<Gallery> galleries;

and each Gallery in galleries have many Brands in it. For example, galleries have 6 Gallery object in it. And each gallery has Brands in it. Like this,

Gallery1.Brandlist => Audi, Ford 
Gallery2.BrandList => Mercedes,Volvo 
Gallery3.BrandList => Subaru 
Gallery4.BrandList => Renault 
Gallery5.BrandList => Subaru 
Gallery6.BrandList =>

What I am trying to get with LINQ is a list of Brands that are distinct of all above’s first brand only(so I won’t take Ford and Volvo even they are in the list). A gallery doesn’t have to have a Brand in their list. So it might be empty as Gallery6. The Output should be,

{Audi, Mercedes, Subaru, Renault}

I don’t know how I can do this with LINQ. I tried SelectMany but all I can do with LINQ is simple (p=>p.Something = (int) something).ToList(). I couldn’t figure out how to do it.

  • 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-06-16T02:10:15+00:00Added an answer on June 16, 2026 at 2:10 am

    Use SelectMany and Distinct:

    IEnumerable<string> allUniqueBrands = allGalleries
        .SelectMany(g => g.BrandList.Select(b => b.Name)).Distinct();
    

    In query syntax:

    IEnumerable<string> allBrands = from gallery in allGalleries
                                    from brand in gallery.BrandList
                                    select brand.Name;
    IEnumerable<string> allUniqueBrands = allBrands.Distinct();
    

    Edit: Now i got it, you need only the first brands of each BrandList.

    If you want to select the Brand you have to provide a custom IEqualityComparer<Brand> which you can use in Distinct. If you neeed a List<Brand>, just call ToList() at the end.

    Here’s an IEqualityComparer<Brand> for Distinct (or Union,Intesect,Except etc):

    public class BrandComparer : IEqualityComparer<Brand>
    {
        public bool Equals(Brand x, Brand y)
        {
            if (x == null || y == null) return false;
            return x.Name.Equals(y.Name, StringComparison.OrdinalIgnoreCase);
        }
    
        public int GetHashCode(Brand obj)
        {
            if (obj == null) return int.MinValue;
            return obj.Name.GetHashCode();
        }
    }
    

    and here’s the distinct list of all (first) brands:

    List<Brand> uniqueFirstBrands = allGalleries
        .Where(g => g.BrandList != null && g.BrandList.Any())
        .Select(g => g.BrandList.First())
        .Distinct(new BrandComparer())
        .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry about the naive question. I have the following: public Array Values {
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Sorry about the wording for my question title. I have a basic HTML anchor
I have a quick question about ARC in iOS. (Sorry I've asked so many
I have a question about the way I should build my website. What if
I don't have much knowledge about the IPv6 protocol, so sorry if the question
(Sorry about the title, couldn't think of how to explain it) So I have
Sorry about the question title, but I couldn't find a more appropriate way to
Sorry about my previous question. The question I have to answer is this: Body
Sorry about the dummy question here. Under Windows I would like to be able

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.