1

我相当肯定这可以重写为 Lambda 表达式,但每次尝试都惨遭失败。我知道,C# Lambda 读起来更干净,但我坚持使用 VB.NET。这是代码-有人可以指出我正确的方向吗?谢谢!

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
4

1 回答 1

1

如果您想将整个东西转换为 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))

不过,老实说,我通常更喜欢只使用它们来过滤结果或构建集合:

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
于 2013-04-05T15:58:33.580 回答