I have a list of Foos that I would like to filter according to foo.HasBar
.
Foo also have a property Baz.
When a Foo is selected, all Foos with the same Baz object should be filtered.
Is it possible to achieve this using a LINQ query or should I use a foreach instead?
Edit: Here's a sample dataset:
Foo.HasBar = true; Foo.Baz = 1;
Foo.HasBar = true; Foo.Baz = 1;
Foo.HasBar = false; Foo.Baz = 1;
Foo.HasBar = true; Foo.Baz = 2;
Foo.HasBar = false; Foo.Baz = 2;
What I'm trying to achieve is that no other iteration on another Foo.HasBar = true; Foo.Baz = 1;
will be performed or no another iteration on Foo.HasBar = false; Foo.Baz = 2;
will be performed if Foo.HasBar = true; Foo.Baz = 2;
was already selected.
Here's how I would have done it with a foreach loop:
var selectedFoos = new List<Foo>();
foreach(var foo in foos)
{
if (selectedFoos.Exists(f => f.Baz == foo.Baz))
continue;
if (foo.HasBar)
selectedFoos.Add(foo);
}