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.
UseSelectManyandDistinct:In query syntax:
Edit: Now i got it, you need only the first brands of each BrandList.
If you want to select the
Brandyou have to provide a customIEqualityComparer<Brand>which you can use inDistinct. If you neeed aList<Brand>, just callToList()at the end.Here’s an
IEqualityComparer<Brand>forDistinct(or Union,Intesect,Except etc):and here’s the distinct list of all (first) brands: