views:

164

answers:

1

I'm having trouble converting a C# Linq statement to VB.NET. I need the following statement converted. I am getting hung up on the i.Sum(v=>v) part.

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}

Here's what I have for VB, but the If(i.Count() = 0, 0, i.Sum()) _ statement says Parameterless aggregate operator 'Sum' is not supported over projections during runtime (no compile time errors). I have also tried i.Sum(Function(q) i.ProductQty) which doesn't work either (says sum cannot be called with these arguments).

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}

Any ideas on how to get that converted to VB.NET and working? Thanks!

+4  A: 

Your "arrow" syntax in C#:

.Sum(v => v)

Can be converted to the following in VB.NET:

.Sum(Function(v) v)

The "arrow" syntax is a lambda expression. For more info on Lambda Expressions in VB.NET, check out this MSDN entry.

Larsenal
FYI, it's called a lambda expression.
Meta-Knight
Yup. Assumed the OP wouldn't know lambdas, but good to note the correct name in there.
Larsenal