none
Convert C# linq to VB.net Linq RRS feed

  • Question

  • 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;
        }
    

    Monday, May 9, 2011 1:24 AM

Answers

  • Hi brealblackman;

    This should be what you are looking for.

    Public Function GetCount() As Integer
    
    	Dim Count As Integer? = (From cartItems in storeDB.Carts
    	             Where cartItems.CartId = ShoppingCartId
    	             Select CType(cartItems.Count, Integer?)).Sum()
      	             
      Return CInt(IIf( count.HasValue, count, 0))
      
    End Function
    
    Public Function GetTotal() As Decimal
    
      Dim total As Decimal? = (From cartItems In storeDB.Carts
                   Where cartItems.CartId = ShoppingCartId
                   Select CType(cartItems.Count, Integer?) * cartItems.Album.Price).Sum()
                   
      Return CDec(IIf( count.HasValue, count, Decimal.Zero))               
    
    End Function
    
    

    Fernando

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, May 9, 2011 3:32 AM