2013-04-05 134 views
1

我相當肯定這可以重寫爲Lambda表達式,但每次嘗試都會失敗。我知道,C#Lambda讀取更清晰,但我堅持使用VB.NET。這裏是代碼 - 有人能指引我朝着正確的方向嗎?謝謝!VB.NET Lambda表達式代替迭代器

Events.Where(Function(e) e.TypeID = 1 AndAlso ("," & e.Values).Contains("," & b.ChoiceID & ",")) _ 
     .ToList() _ 
     .ForEach(Sub(e) Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine & _ 
       "User: " & cacheValues.CurrentUserName & Environment.NewLine & _ 
       "Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString)) 

老實說,雖然,我通常喜歡只用它來過濾下來的結果,或建立一個集合:如果你想整個事情轉化爲拉姆達

For Each e As EventToMonitor In Events 
    If e.TypeID = 1 Then 
     If ("," & e.Values).Contains("," & b.ChoiceID & ",") Then 
      Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine & 
         "User: " & cacheValues.CurrentUserName & Environment.NewLine & _ 
         "Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString) 
     End If 
    End If 
Next 
+0

這是一個鍵盤記錄? – APrough 2013-04-05 15:55:58

+0

在這種情況下,你想要* lambda *爲什麼?目前的代碼很好。也就是說,我會使用LINQ表達式來過濾(而不是'If')和變換(而不是'Notify'調用中的串聯),然後迭代結果,調用'Notify(cacheValues,value) '對於每一個,其中'value'是迭代變量。 – 2013-04-05 16:02:45

回答

1

像這樣的東西應該工作:

Dim eventsList = Events.Where(Function(e) e.TypeID = 1 AndAlso ("," & e.Values).Contains("," & b.ChoiceID & ",")) 

For Each e As EventToMonitor In eventsList 
    Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine & 
       "User: " & cacheValues.CurrentUserName & Environment.NewLine & _ 
       "Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString) 
Next 
+0

同意,後面的例子是我也會去的路線。這樣更清楚。 – 2013-04-05 16:13:28