Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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.

C#
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();
     return total ?? decimal.Zero;
   }
Posted
Updated 8-May-11 17:39pm
v2

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.
 
Share this answer
 
I have figured it out and most free code converter cannot convert LINQ

VB
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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900