0

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!

1
  • 3
    Note that if you want an accurate translation, && in C# is equivalent to AndAlso not And. Similarly || is equivalent to OrElse not Or. Commented Oct 20, 2009 at 18:30

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.