Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# LINQ VB.NET
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();
      return total ?? decimal.Zero;
    }
Posted 8-May-11 15:47pm
Edited 8-May-11 17:39pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 435
1 Maciej Los 335
2 OriginalGriff 278
3 CHill60 180
4 Sinisa Hajnal 180


Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 9 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100