12,819,402 members (31,715 online)
Rate this:
See more:
I am fairly new to some of the linq functionality and I was doing an example to improve my MVC skills and ran into this code I couldn't figure it out. I am using vb.net but the code came in C#. I couldn't figure out how to convert the linq version.

``` public int GetCount()
{
// Get the count of each item in the cart and sum them up
int? count = (from cartItems in storeDB.Carts
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count).Sum();
// Return 0 if all entries are null
return count ?? 0;
}
public decimal GetTotal()
{
// Multiply album price by count of that album to get
// the current price for each of those albums in the cart
// sum all album price totals to get the cart total
decimal? total = (from cartItems in storeDB.Carts
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count *
cartItems.Album.Price).Sum();
}```
Posted 8-May-11 16:47pm
Updated 8-May-11 18:39pm
v2

Rate this:

Solution 1

Use this online tool for such work @ http://www.developerfusion.com/tools/convert/csharp-to-vb/[^]
It converts code from C# to VB.NET and vice versa.
Rate this:

Solution 2

I have figured it out and most free code converter cannot convert LINQ

```Public Function GetCount() As Integer
' Get the count of each item in the cart and sum them up
Dim count As System.Nullable(Of Integer) = (From cartItems In storeDB.Carts Where cartItems.CartId = ShoppingCartId Select (CType(cartItems.Count, System.Nullable(Of Integer)))).Sum()
' Return 0 if all entries are null
Return If(count, 0)
End Function
Public Function GetTotal() As Decimal
' Multiply album price by count of that album to get
' the current price for each of those albums in the cart
' sum all album price totals to get the cart total
Dim total As System.Nullable(Of Decimal) = (From cartItems In storeDB.Carts Where cartItems.CartId = ShoppingCartId Select CType(cartItems.Count, System.Nullable(Of Integer)) * cartItems.Album.Price).Sum()
Return If(total, Decimal.Zero)
End Function```
v3

Top Experts
Last 24hrsThis month
 OriginalGriff 230 Jochen Arndt 120 Richard Deeming 90 Peter Leow 75 Karthik Bangalore 70
 OriginalGriff 4,697 Graeme_Grant 3,814 Karthik Bangalore 3,501 Jochen Arndt 2,571 ppolymorphe 2,477