LINQ至關於IEnumerable的foreach

我想在LINQ中執行如下操做,可是我不知道如何操做: app

IEnumerable<Item> items = GetItems();
items.ForEach(i => i.DoStuff());

真正的語法是什麼? spa


#1樓

我尊重不一樣的觀點,即連接擴展方法應無反作用(不只由於它們不是,任何委託均可以執行反作用)。 code

考慮如下: orm

public class Element {}

   public Enum ProcessType
   {
      This = 0, That = 1, SomethingElse = 2
   }

   public class Class1
   {
      private Dictionary<ProcessType, Action<Element>> actions = 
         new Dictionary<ProcessType,Action<Element>>();

      public Class1()
      {
         actions.Add( ProcessType.This, DoThis );
         actions.Add( ProcessType.That, DoThat );
         actions.Add( ProcessType.SomethingElse, DoSomethingElse );
      }

      // Element actions:

      // This example defines 3 distict actions
      // that can be applied to individual elements,
      // But for the sake of the argument, make
      // no assumption about how many distict
      // actions there may, and that there could
      // possibly be many more.

      public void DoThis( Element element )
      {
         // Do something to element
      }

      public void DoThat( Element element )
      {
         // Do something to element
      }

      public void DoSomethingElse( Element element )
      {
         // Do something to element
      }

      public void Apply( ProcessType processType, IEnumerable<Element> elements )
      {
         Action<Element> action = null;
         if( ! actions.TryGetValue( processType, out action ) )
            throw new ArgumentException("processType");
         foreach( element in elements ) 
            action(element);
      }
   }

該示例所顯示的實際上只是一種後期綁定,它容許一個調用對元素序列產生反作用的許多可能動做之一,而無需編寫大型的switch構造來解碼定義該動做並轉換的值將其轉換爲相應的方法。 ip


#2樓

這種「功能方法」的抽象會浪費大量時間。 語言層面上沒有任何東西能夠防止反作用。 只要您可使其對容器中的每一個元素都調用lambda / delegate-您將得到「 ForEach」行爲。 element

例如,這裏將srcDictionary合併到destDictionary中的一種方法(若是密鑰已經存在-覆蓋) get

這是一個hack,不該在任何生產代碼中使用。 it

var b = srcDictionary.Select(
                             x=>
                                {
                                  destDictionary[x.Key] = x.Value;
                                  return true;
                                }
                             ).Count();

#3樓

不少人提到它,可是我不得不寫下來。 這不是最清晰/最易讀的嗎? io

IEnumerable<Item> items = GetItems();
foreach (var item in items) item.DoStuff();

簡短(st)。 form


#4樓

讓您的反作用遠離個人IEnumerable

我想在LINQ中執行如下操做,可是我不知道如何操做:

正如其餘人指出, 這裏和國外LINQ和IEnumerable方法,預計是免費的反作用。

您是否真的要對IEnumerable中的每一個項目執行「操做」? 那麼foreach是最佳選擇。 當這裏發生反作用時,人們並不感到驚訝。

foreach (var i in items) i.DoStuff();

我敢打賭你不要反作用

可是根據個人經驗,一般不須要反作用。 Jon Skeet,Eric Lippert或Marc Gravell經常會解釋一個簡單的LINQ查詢,並等待StackOverflow.com的回答,解釋如何作本身想作的!

一些例子

若是您實際上只是在彙總(累積)某個值,則應考慮「 Aggregate擴展方法。

items.Aggregate(initial, (acc, x) => ComputeAccumulatedValue(acc, x));

也許您想根據現有值建立一個新的IEnumerable

items.Select(x => Transform(x));

或者,也許您想建立一個查找表:

items.ToLookup(x, x => GetTheKey(x))

可能性的清單(並不是徹底是雙關語)不斷出現。


#5樓

對於VB.NET,您應該使用:

listVariable.ForEach(Sub(i) i.Property = "Value")
相關文章
相關標籤/搜索