linq的group by一个例子

前端之家收集整理的这篇文章主要介绍了linq的group by一个例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

[原问题转自]http://topic.csdn.net/u/20120118/14/14bba2f0-1a60-4c6c-a211-fcb6c6da709c.html?12661

有如下表

T_UserConsumeDay

UserConsumeMoney ConsumeDay
20.0000 2011-12-1
23.0100 2011-11-1
25.0120 2012-1-1
28.0000 2011-12-2

我需要按月统计UserConsumeMoney的总额,预计结果如下

totalMoney monthConsume
23.0100 2011-11
48.0000 2011-12
25.0120 2012-01

C#代码

            var query = from q in db.T_UserConsumeDay.ToList()
                        group q by q.ConsumeDay.Value.ToString("yyyy-MM") into g
                        orderby g.Key
                        select new
                        {
                            money = g.Sum(r => r.UserConsumeMoney),month = g.Key
                        };

VB.NET代码 //vb.net代码比较麻烦,可能是我水平有限,如果有高人知道,请指点.
        Dim query = From q In db.T_UserConsumeDay.ToList() _
                    Select r = New With { _
                    .month = q.ConsumeDay.Value.ToString("yyyy-MM"),_
                    .money = q.UserConsumeMoney} _
                    Group r By r.month Into Group = Sum(r.money) _
                    Order By month _
                    Select New With {.totalMoney = Group,.monthConsume = month}
原文链接:https://www.f2er.com/vb/260425.html

猜你在找的VB相关文章