Using LINQ to get all property with the exact same value?

Viewed 7686

I have a list of data like this:

Prod1: Id=1,  Name="Book",    Active=true
Prod2: Id=2,  Name="Book",    Active=false
Prod3: Id=3,  Name="Book",    Active=true
Prod4: Id=4,  Name="Laptop",  Active=true
Prod5: Id=5,  Name="Laptop",  Active=true
Prod6: Id=6,  Name="Laptop",  Active=true

What I want to perform is to get reduced list like this:

Prod1: Id=4, Name="Laptop", Active=true

What I am trying to do is that I need to select all products group by its Name, and return all that has true. Since Book has one false, it should not return Book.

I've tried this one:

lstProducts = lstProducts 
                .Where(x =>
                    lstProducts 
                   .All(c => c.Name == x.Name && c.Active == true))
                .GroupBy(c => c.Name).Select(c => c.First())
                .ToList();

But its returning zero results. If I do a where clause where Active == true, its getting a Book product, which shouldn't since all of its Active should be true in order to get it.

2 Answers
Related