LINQ is not all that is useful for performing side effects; it is primarily intended for queries. In truth, the fact that he postponed a performance so rooted in his behavior makes him a poor choice for side effects, IMO.
, , . LINQ, , :
foreach (var o in obj.Where(i => i.SomeProperty == Something))
{
o.SomeOtherProperty = true;
}
, , (, ), .
, , , - :
var projection = obj.Where(i => i.SomeProperty == Something)
.Select(i => new Foo(i) { SomeOtherProperty = true });
EDIT. (.: Eric Lippert), :
public static void Do<T>(this IEnumerable<T> source, Action<T> action)
{
if (source == null)
throw new ArgumentNullException("source");
if (action == null)
throw new ArgumentNullException("action");
foreach (T item in source)
action(item);
}
:
obj.Where(o => o.SomeProperty == Something)
.Do(o => o.SomeOtherProperty = true);