Reputation: 181
I have a command to select the items from an observable collection.
Items.Select(a => a.MyParameter).ToList()
Is there a way to extend this line to say "but not MyParameter==IsBad?"
EDIT: I apologize for not asking a clearer question.
Select all of the items.MyDoubleValue but not the items that have items.Exclude==true. This goes to a list in one case and it goes to sum in another. I have to add the boolean Exclude to all of the items so I have to edit the summing, averaging, standard deviations, etc. I just wanted to avoid multi line code if I can.
Upvotes: 0
Views: 3163
Reputation: 233
This is called Using Linq, and its one of my favorite features in C# because it reminds me of Python.
When you write:
Items.Select(a => a.MyParameter).ToList();
The use of the lambda/delegate syntax causes a to become an element of your collection, Items.
The elements in Items must have some parameter in it called a.MyParameter. Hence, this above line of code simply grabs all of the MyParameter objects.
If you wanted to filter certain values of MyParameter, you should use Where instead:
Items.Where(a => a.MyParameter != IsBad).ToList();
In short, use Select to transform every element, but use Where to filter. And if you wanted to, you could also combine both, depending on what you want in the end.
Upvotes: 2
Reputation: 14477
You can filter the sequence through Where
:
Items
.Select(a => a.MyParameter)
.Where(p => !p.IsBad)
.ToList()
Upvotes: 2