If you want to conditionally project Foo onto another Foo (leaving the others untouched), you can do something like:
IEnumerable<Foo> foos = ...
var transformed = foos.Select(foo => myCondition(foo) ? transform(foo) : foo);
On the other hand, if you only want to design Foos that meet the condition:
var transformed = foos.Where(foo => myCondition(foo))
.Select(foo => transform(foo));
, - LINQ . , , .
foos = foos.Select(foo => transform(foo)).ToList();
, , LINQ, - List<T>.ConvertAll:
List<Foo> foos = ...
var transformed = foos.ConvertAll
(foo => myCondition(foo) ? transform(foo) : foo);
EDIT. , "ReplaceWhere" - , framework, . , :
public static void ReplaceWhere<T>(this IList<T> list,
Func<T, bool> predicate,
Func<T, T> selector)
{
for (int i = 0; i < list.Count; i++)
{
T item = list[i];
if (predicate(item))
list[i] = selector(item);
}
}
List<int> myList = ...
myList.ReplaceWhere(i => i > 0, i => i * i);