LINQ equivalent of foreach for IEnumerable

后端 未结 22 2253
夕颜
夕颜 2020-11-21 22:54

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         


        
22条回答
  •  感情败类
    2020-11-21 23:31

    There is no ForEach extension for IEnumerable; only for List. So you could do

    items.ToList().ForEach(i => i.DoStuff());
    

    Alternatively, write your own ForEach extension method:

    public static void ForEach(this IEnumerable enumeration, Action action)
    {
        foreach(T item in enumeration)
        {
            action(item);
        }
    }
    

提交回复
热议问题