将 C# lambda 函数转换为 VB.net
我有一个将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.NET 3.5 中的 IIRC VB.NET 不支持带有主体的匿名函数。使用 VB.NET 的同事必须定义一个包含此代码的函数,并在 lambda 表达式中使用此函数。现在,话虽这么说,在这种情况下,实际上没有必要使用带有 body 的复杂函数,并且此代码可以简化为:
通常,如果我的 VB.NET 不是太生疏,应该看起来类似于:
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:
which normally if my VB.NET isn't too rusty should look something along the lines of:
简单:
在较新版本的 VB 中,您可以省略烦人的显式续行符 (
_
)。Easy:
In newer versions of VB you can omit the annoying explicit line continuations (
_
).我刚刚尝试过,转换代码没有问题:
http://www .developerfusion.com/tools/convert/csharp-to-vb/
唯一需要注意的是 VB.NET 9 (VS 2008/.NET 3.5) 不支持多行 lambda表达式。所以你不能把它分成两行。
所以...
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...