将C#linq转换为VB.net Linq [英] Convert C# linq to VB.net Linq

查看:70
本文介绍了将C#linq转换为VB.net Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对linq的某些功能还很陌生,我正在举一个例子来提高我的MVC技能,并遇到了我无法弄清楚的代码.我正在使用vb.net,但代码来自C#.我不知道如何转换linq版本.

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

推荐答案

将此在线工具用于此类工作@
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.


我知道了,大多数免费代码转换器都无法转换LINQ

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


这篇关于将C#linq转换为VB.net Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆