Is there such a structure in C# where I could say something similar to the following:
foreach(object obj in listofObjects where obj.property == false){
so that it would only iterate through a specific subset of objects in the collection?
It's simple with extension mehtods:
foreach(object obj in listofObjects.Where(w => !w.property))
You can use the method syntax
foreach(object obj in listofObjects.Where(obj => !obj.property))
It is also possible using the query syntax but it's not readable (to me at least):
foreach(object obj in (from x in listofObjects where !x.property select x))
If you are gonna use that I would store the query into a variable:
var query = (from x in listofObjects
where !x.property
select x);
foreach(var obj in query) { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With