求一个linq语句怎么写

likedefly 2012-12-03 10:06:53
原表:
login Goods Price Qty Bos Date

meimei 铜 2.12 2 B 2012-12-03
meimei 铜 2.12 5 S 2012-12-03
meimei 铜 2.14 2 S 2012-12-03
meimei 铝 4.63 2 B 2012-12-03
meimei 铝 4.62 2 B 2012-12-03
gege 金 432 6 S 2012-12-03

结果:

login Goods TotalPrice TotalQty BQty SQty Date

meimei 铜 6.38 9 2 7 2012-12-03
meimei 铝 9.25 4 4 0 2012-12-03
gege 金 432 6 0 6 2012-12-03

求高效的linq语句,请大虾帮忙
...全文
184 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2012-12-04
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/370024120
你的选择B 2012-12-04
  • 打赏
  • 举报
回复
失误,用错了函数: 1:如下

                var result = from u in entity.table
                             group u by new { u.login, u.Goods, u.Date } into temp
                             select new
                             {
                                 login = temp.Key.login,
                                 Goods = temp.Key.Goods,
                                 TotalPrice = temp.Sum(c => c.Price),
                                 TotalQty = temp.Sum(c => c.Qty),
                                 BQty = temp.Where(c => c.Key == "B").Sum(c => c.Qty),
                                 SQty = temp.Where(c => c.Key == "S").Sum(c => c.Qty),
                                 Date = temp.FirstOrDefault().Date
                             };
2:如下

                using (NorthwindEntities entity = new NorthwindEntities())
                {
                    var result = from u in entity.table
                                 group u by new { u.login, u.Goods, u.Date } into temp
                                 select new
                                 {
                                     login = temp.Key.login,
                                     Goods = temp.Key.Goods,
                                     TotalPrice = temp.Sum(c => c.Price),
                                     TotalQty = temp.Sum(c => c.Qty),
                                     BQty = temp.GroupBy(c => c.Bos).Where(c => c.Key == "B").Sum(c => c.Qty),
                                     SQty = temp.GroupBy(c => c.Bos).Where(c => c.Key == "S").Sum(c => c.Qty),
                                     Date = temp.FirstOrDefault().Date
                                 };
                }
你的选择B 2012-12-04
  • 打赏
  • 举报
回复
或者内部没必要再进行一次分组 直接查出count即可 修改如下:

                var result = from u in entity.table
                             group u by new { u.login, u.Goods, u.Date } into temp
                             select new
                             {
                                 login = temp.Key.login,
                                 Goods = temp.Key.Goods,
                                 TotalPrice = temp.Sum(c => c.Price),
                                 TotalQty = temp.Sum(c => c.Qty),
                                 BQty = temp.Where(c => c.Key == "B").Count(),
                                 SQty = temp.Where(c => c.Key == "S").Count(),
                                 Date = temp.FirstOrDefault().Date
                             };

你的选择B 2012-12-04
  • 打赏
  • 举报
回复
手写,仅供参考

            using (NorthwindEntities entity = new NorthwindEntities())
            {
                var result = from u in entity.table
                             group u by new { u.login, u.Goods, u.Date } into temp
                             select new
                             {
                                 login = temp.Key.login,
                                 Goods = temp.Key.Goods,
                                 TotalPrice = temp.Sum(c => c.Price),
                                 TotalQty = temp.Sum(c => c.Qty),
                                 BQty = temp.GroupBy(c => c.Bos).Where(c => c.Key == "B").Count(),
                                 SQty = temp.GroupBy(c => c.Bos).Where(c => c.Key == "S").Count(),
                                 Date = temp.FirstOrDefault().Date
                             };
            }
  • 打赏
  • 举报
回复
var query = from t in dc.Class1 group t by new { t.login, t.Goods } into g select new { login = g.Key.login, Goods = g.Key.Goods, TotalPrice = g.Sum(x => x.Price), TotalQty = g.Sum(x => x.Qty), BQty = g.Sum(x => x.Qty.ToString().Trim() == "B" ? 1 : 0), SQty = g.Sum(x => x.Qty.ToString().Trim() == "S" ? 1 : 0), Date = g.Max(x => x.Date) };
likedefly 2012-12-03
  • 打赏
  • 举报
回复
login Goods Price date group by取sum,同时对Qty根据Bos(login Goods Price date这几个字段依然有效)求和。
机器人 2012-12-03
  • 打赏
  • 举报
回复
就是按照日期 Goods, Date GroupBy 取 Sum 。。。

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧