I\'d like to do the equivalent of the following in LINQ, but I can\'t figure out how:
IEnumerable- items = GetItems();
items.ForEach(i => i.DoS
You could use the FirstOrDefault()
extension, which is available for IEnumerable
. By returning false
from the predicate, it will be run for each element but will not care that it doesn't actually find a match. This will avoid the ToList()
overhead.
IEnumerable- items = GetItems();
items.FirstOrDefault(i => { i.DoStuff(); return false; });