Linq to Sql - 将C#转换为VB.NET帮助

时间:2009-10-20 18:24:09

标签: vb.net linq linq-to-sql

我在将C#Linq语句转换为VB.NET时遇到问题。我需要转换以下语句。我在i.Sum(v => v)部分被挂断了。

from p in Products
from u in Users
let i = (from op in OrderProducts
         where op.Order.User == u && op.Product == p
         select op.ProductQty)
let quant = i.Count() == 0 ? 0 : i.Sum(v=>v)
select new {p.ProductName, u.UserName, Quantity = quant}

这是我对VB的看法,但If(i.Count()= 0,0,i.Sum())_语句表示在运行期间不支持无参数聚合运算符'Sum'(无编译时)错误)。我也尝试了i.Sum(函数(q)i.ProductQty),它也不起作用(用不能用这些参数调用sum)。

From p In Products _
From u In Users _
Let i = (From op In OrderProducts _
         Where op.Order.User.UserID = u.UserID And op.Product.ProductID = p.ProductID _
         Select op.ProductQty) _
Let Qty = _
         If(i.Count() = 0, 0, i.Sum()) _
Select New With {p.ProductName, u.Username, Qty}

有关如何将其转换为VB.NET并运行的任何想法?谢谢!

1 个答案:

答案 0 :(得分:6)

C#中的“箭头”语法:

.Sum(v => v)

可以在VB.NET中转换为以下内容:

.Sum(Function(v) v)

“箭头”语法是lambda表达式。有关VB.NET中Lambda表达式的更多信息,请查看this MSDN entry