将 C# lambda 函数转换为 VB.net

发布于 2024-12-08 17:09:31 字数 780 浏览 4 评论 0原文

我有一个将 IDataReader 映射到类的函数。明显是用C#写的。我的同事想在他的代码中使用相同的方法,但他是用 VB.net 编写的。基本上,由于 C# 中使用的 Lambda 表达式,我很难重写它。他正在运行.Net 3.5。

谁能帮助我吗?

private Func<IDataReader, ScanItem> resultMapper = r =>
    {
        var si = new ScanItem()
        {
            StoreGroupCode = r.ToInt32("GRP_CDE"),
            StoreCode = r.ToInt32("STOR_CDE"),
            EventNumber = r.ToInt32("EVENT_NUM"),
            AreaNumber = r.ToInt32("INV_CTL_AREA_CDE"),
            LabelNumber = r.ToInt32("LBL_NUM"),
            ScanType = r.ToString("INV_SCAN_TYP_IND"),
            SequenceNumber = r.ToInt32("INV_SCAN_SEQ_NUM"),
            UPC = r.ToLong("VEN_UPC_NUM"),
            ActualQuantity = r.ToLong("ACT_CNT_QTY")
        };

        return si;
    };

I have this function that maps a IDataReader to a class. It is obviously written in C#. My co-worker wants to use the same method in his code, but he is writing in VB.net. Basically I am having difficulty rewriting this due to the Lambda expressions used in C#. He is running .Net 3.5.

Can anyone help me?

private Func<IDataReader, ScanItem> resultMapper = r =>
    {
        var si = new ScanItem()
        {
            StoreGroupCode = r.ToInt32("GRP_CDE"),
            StoreCode = r.ToInt32("STOR_CDE"),
            EventNumber = r.ToInt32("EVENT_NUM"),
            AreaNumber = r.ToInt32("INV_CTL_AREA_CDE"),
            LabelNumber = r.ToInt32("LBL_NUM"),
            ScanType = r.ToString("INV_SCAN_TYP_IND"),
            SequenceNumber = r.ToInt32("INV_SCAN_SEQ_NUM"),
            UPC = r.ToLong("VEN_UPC_NUM"),
            ActualQuantity = r.ToLong("ACT_CNT_QTY")
        };

        return si;
    };

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

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

发布评论

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

评论(3

尾戒 2024-12-15 17:09:31

.NET 3.5 中的 IIRC VB.NET 不支持带有主体的匿名函数。使用 VB.NET 的同事必须定义一个包含此代码的函数,并在 lambda 表达式中使用此函数。现在,话虽这么说,在这种情况下,实际上没有必要使用带有 body 的复杂函数,并且此代码可以简化为:

private Func<IDataReader, ScanItem> resultMapper = r => new ScanItem
{
    StoreGroupCode = r.ToInt32("GRP_CDE"),
    StoreCode = r.ToInt32("STOR_CDE"),
    EventNumber = r.ToInt32("EVENT_NUM"),
    AreaNumber = r.ToInt32("INV_CTL_AREA_CDE"),
    LabelNumber = r.ToInt32("LBL_NUM"),
    ScanType = r.ToString("INV_SCAN_TYP_IND"),
    SequenceNumber = r.ToInt32("INV_SCAN_SEQ_NUM"),
    UPC = r.ToLong("VEN_UPC_NUM"),
    ActualQuantity = r.ToLong("ACT_CNT_QTY")
};

通常,如果我的 VB.NET 不是太生疏,应该看起来类似于:

Private resultMapper As Func(Of IDataReader, ScanItem) = Function(r) New ScanItem() With { _
    .StoreGroupCode = r.ToInt32("GRP_CDE"), _
    .StoreCode = r.ToInt32("STOR_CDE"), _
    .EventNumber = r.ToInt32("EVENT_NUM"), _
    .AreaNumber = r.ToInt32("INV_CTL_AREA_CDE"), _
    .LabelNumber = r.ToInt32("LBL_NUM"), _
    .ScanType = r.ToString("INV_SCAN_TYP_IND"), _
    .SequenceNumber = r.ToInt32("INV_SCAN_SEQ_NUM"), _
    .UPC = r.ToLong("VEN_UPC_NUM"), _
    .ActualQuantity = r.ToLong("ACT_CNT_QTY") _
}

IIRC VB.NET in .NET 3.5 doesn't support anonymous functions with body. Your co-worker using VB.NET will have to define a function containing this code and in the lambda expression use this function. Now, this being said, it's not really necessary to use a complex function with body in this case and this code could be simplified to:

private Func<IDataReader, ScanItem> resultMapper = r => new ScanItem
{
    StoreGroupCode = r.ToInt32("GRP_CDE"),
    StoreCode = r.ToInt32("STOR_CDE"),
    EventNumber = r.ToInt32("EVENT_NUM"),
    AreaNumber = r.ToInt32("INV_CTL_AREA_CDE"),
    LabelNumber = r.ToInt32("LBL_NUM"),
    ScanType = r.ToString("INV_SCAN_TYP_IND"),
    SequenceNumber = r.ToInt32("INV_SCAN_SEQ_NUM"),
    UPC = r.ToLong("VEN_UPC_NUM"),
    ActualQuantity = r.ToLong("ACT_CNT_QTY")
};

which normally if my VB.NET isn't too rusty should look something along the lines of:

Private resultMapper As Func(Of IDataReader, ScanItem) = Function(r) New ScanItem() With { _
    .StoreGroupCode = r.ToInt32("GRP_CDE"), _
    .StoreCode = r.ToInt32("STOR_CDE"), _
    .EventNumber = r.ToInt32("EVENT_NUM"), _
    .AreaNumber = r.ToInt32("INV_CTL_AREA_CDE"), _
    .LabelNumber = r.ToInt32("LBL_NUM"), _
    .ScanType = r.ToString("INV_SCAN_TYP_IND"), _
    .SequenceNumber = r.ToInt32("INV_SCAN_SEQ_NUM"), _
    .UPC = r.ToLong("VEN_UPC_NUM"), _
    .ActualQuantity = r.ToLong("ACT_CNT_QTY") _
}
┼── 2024-12-15 17:09:31

简单:

Private resultMapper As Func(Of IDataReader, ScanItem) = Function (r) _
    New ScanItem() With { _
        .StoreGroupCode = r.ToInt32("GRP_CDE"), _
        .StoreCode = r.ToInt32("STOR_CDE"), _
        .EventNumber = r.ToInt32("EVENT_NUM"), _
        .AreaNumber = r.ToInt32("INV_CTL_AREA_CDE"), _
        .LabelNumber = r.ToInt32("LBL_NUM"), _
        .ScanType = r.ToString("INV_SCAN_TYP_IND"), _
        .SequenceNumber = r.ToInt32("INV_SCAN_SEQ_NUM"), _
        .UPC = r.ToLong("VEN_UPC_NUM"), _
        .ActualQuantity = r.ToLong("ACT_CNT_QTY") _
    }

在较新版本的 VB 中,您可以省略烦人的显式续行符 (_)。

Easy:

Private resultMapper As Func(Of IDataReader, ScanItem) = Function (r) _
    New ScanItem() With { _
        .StoreGroupCode = r.ToInt32("GRP_CDE"), _
        .StoreCode = r.ToInt32("STOR_CDE"), _
        .EventNumber = r.ToInt32("EVENT_NUM"), _
        .AreaNumber = r.ToInt32("INV_CTL_AREA_CDE"), _
        .LabelNumber = r.ToInt32("LBL_NUM"), _
        .ScanType = r.ToString("INV_SCAN_TYP_IND"), _
        .SequenceNumber = r.ToInt32("INV_SCAN_SEQ_NUM"), _
        .UPC = r.ToLong("VEN_UPC_NUM"), _
        .ActualQuantity = r.ToLong("ACT_CNT_QTY") _
    }

In newer versions of VB you can omit the annoying explicit line continuations (_).

趁微风不噪 2024-12-15 17:09:31

我刚刚尝试过,转换代码没有问题:

http://www .developerfusion.com/tools/convert/csharp-to-vb/

唯一需要注意的是 VB.NET 9 (VS 2008/.NET 3.5) 不支持多行 lambda表达式。所以你不能把它分成两行。

所以...

Private resultMapper As Func(Of IDataReader, ScanItem) = Function(r) New ScanItem() With { _
    .StoreGroupCode = r.ToInt32("GRP_CDE"), _
    .StoreCode = r.ToInt32("STOR_CDE"), _
    .EventNumber = r.ToInt32("EVENT_NUM"), _
    .AreaNumber = r.ToInt32("INV_CTL_AREA_CDE"), _
    .LabelNumber = r.ToInt32("LBL_NUM"), _
    .ScanType = r.ToString("INV_SCAN_TYP_IND"), _
    .SequenceNumber = r.ToInt32("INV_SCAN_SEQ_NUM"), _
    .UPC = r.ToLong("VEN_UPC_NUM"), _
    .ActualQuantity = r.ToLong("ACT_CNT_QTY") _
}

I just tried this and it had no problem converting the code:

http://www.developerfusion.com/tools/convert/csharp-to-vb/

The only thing to beware of is VB.NET 9 (VS 2008/.NET 3.5) doesn't support multi-line lambda expressions. So you can't split it into 2 lines.

So...

Private resultMapper As Func(Of IDataReader, ScanItem) = Function(r) New ScanItem() With { _
    .StoreGroupCode = r.ToInt32("GRP_CDE"), _
    .StoreCode = r.ToInt32("STOR_CDE"), _
    .EventNumber = r.ToInt32("EVENT_NUM"), _
    .AreaNumber = r.ToInt32("INV_CTL_AREA_CDE"), _
    .LabelNumber = r.ToInt32("LBL_NUM"), _
    .ScanType = r.ToString("INV_SCAN_TYP_IND"), _
    .SequenceNumber = r.ToInt32("INV_SCAN_SEQ_NUM"), _
    .UPC = r.ToLong("VEN_UPC_NUM"), _
    .ActualQuantity = r.ToLong("ACT_CNT_QTY") _
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文