Lambda 教程和求解 Lambda 函数

发布于 2024-08-14 14:34:39 字数 511 浏览 2 评论 0原文

是否可以将以下函数缩短为 lambda 表达式?

或者(我自己做这个技巧)对于初学者来说,vb.net 中的 lambda 最好、最容易理解的教程是什么?

Function getit(ByVal wert As Integer, ByVal sk As Integer, ByVal list As List(Of Array)) As String
    Dim ergebnis As String
    ergebnis = "Null"
    For Each strg As String() In list
        If wert >= Integer.Parse(strg(0)) And wert < Integer.Parse(strg(0)) + 5 And sk = Integer.Parse(strg(1)) Then
            Return strg(2)
        End If
    Next
    Return ergebnis
End Function

Is it possible to shorten the following function to a lambda expression?

Or (to do the trick by myself) what is the best and most understandable for beginners tutorial for lambda in vb.net?

Function getit(ByVal wert As Integer, ByVal sk As Integer, ByVal list As List(Of Array)) As String
    Dim ergebnis As String
    ergebnis = "Null"
    For Each strg As String() In list
        If wert >= Integer.Parse(strg(0)) And wert < Integer.Parse(strg(0)) + 5 And sk = Integer.Parse(strg(1)) Then
            Return strg(2)
        End If
    Next
    Return ergebnis
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2024-08-21 14:34:39

您可以创建一个 lambda 表达式,它接受一个字符串数组,如果满足条件则返回 True:

Dim isValidArray = Function(strg as String()) _
                     wert >= Integer.Parse(strg(0)) AndAlso _
                     wert < Integer.Parse(strg(0)) + 5 AndAlso _
                     sk = Integer.Parse(strg(1))

我还会更改方法的签名以接受字符串数组列表而不是任何数组列表。最终的代码是:

Function getit(ByVal wert As Integer, ByVal sk As Integer, _
               ByVal list As List(Of String())) As String

    ''//Insert above lambda here

    ''//Get first valid item or default (Nothing) if no valid item
    Dim validArray As String() = list.FirstOrDefault(isValidArray)

    If validArray IsNot Nothing
        Return validArray(2)
    Else
        Return "Null"
    End If
End Function

You could create a lambda expression which takes a string array and return True if it fulfills the condition:

Dim isValidArray = Function(strg as String()) _
                     wert >= Integer.Parse(strg(0)) AndAlso _
                     wert < Integer.Parse(strg(0)) + 5 AndAlso _
                     sk = Integer.Parse(strg(1))

I would also change the signature of your method to accept a list of string array instead of a list of any array. The final code would be:

Function getit(ByVal wert As Integer, ByVal sk As Integer, _
               ByVal list As List(Of String())) As String

    ''//Insert above lambda here

    ''//Get first valid item or default (Nothing) if no valid item
    Dim validArray As String() = list.FirstOrDefault(isValidArray)

    If validArray IsNot Nothing
        Return validArray(2)
    Else
        Return "Null"
    End If
End Function
青瓷清茶倾城歌 2024-08-21 14:34:39

尝试一下:

Dim getit As String;

getit = (From x In list
         Where (wert >= Integer.Parse(x(0))) AndAlso (wert < Integer.Parse(x(0)) + 5) AndAlso (sk = Integer.Parse(x(1)))
         Select x(2)).Union(Of String)(New List(Of String) { "Null" }).First(Of String)()

Give this a try:

Dim getit As String;

getit = (From x In list
         Where (wert >= Integer.Parse(x(0))) AndAlso (wert < Integer.Parse(x(0)) + 5) AndAlso (sk = Integer.Parse(x(1)))
         Select x(2)).Union(Of String)(New List(Of String) { "Null" }).First(Of String)()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文