一个Linq Group By 和Sum的范例
来源:互联网 发布:autodesk系列软件 编辑:程序博客网 时间:2024/05/21 14:33
转自:http://www.cnblogs.com/sskset/archive/2012/09/06/2674055.html
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace ConsoleApplication3{ class Program { static void Main(string[] args) { var Items = new List<SortItem>(); Items.AddRange(new SortItem[] { new SortItem(){ GroupId = 1, Id=1, Count = 1, Weight = 100 }, new SortItem(){ GroupId = 1, Id=1, Count = 2, Weight = null }, new SortItem(){ GroupId = 1, Id =2, Count = 3, Weight = 300 }, new SortItem(){ GroupId = 2, Id=1, Count = 2, Weight = 200 }, new SortItem(){ GroupId = 2, Id=2, Count = 2, Weight = 200 }, new SortItem(){ GroupId = 2, Id=3, Count = 4, Weight = null }, new SortItem(){ GroupId = 3, Id=3 ,Count = 2, Weight = null }, new SortItem(){ GroupId = 1, Id =2, Count = 6, Weight = 300 }, }); var data = (from a in Items group a by new { a.GroupId, a.Id } into b orderby new ComparerItem() { GroupId = b.Key.GroupId, Id = b.Key.Id } descending select new { GroupId = b.Key.GroupId, Id = b.Key.Id, Count = b.Sum(c => c.Count), Weight = b.Sum(c => c.Weight) }).OrderBy(t => t.GroupId).ThenBy(t => t.Id); foreach (var anItem in data) Console.WriteLine("GroupId:{0} Id:{1} Count:{2} Weight:{3}", anItem.GroupId, anItem.Id, anItem.Count, anItem.Weight); Console.ReadKey(); } class ComparerItem : IComparable { public int GroupId { get; set; } public int Id { get; set; } public int CompareTo(object obj) { var tem = (ComparerItem)obj; if (this.GroupId == tem.GroupId) { if (this.Id > tem.Id) return 1; else if (this.Id < tem.Id) return -1; return 0; } else if (this.GroupId > tem.GroupId) { return 1; } return -1; } } class SortItem { public int GroupId { get; set; } public int Id { get; set; } public int Count { get; set; } public int? Weight { get; set; } } }}
0 0
- 一个Linq Group By 和Sum的范例
- 一个Linq Group By 和Sum的范例
- linq的group by一个例子
- Linq中Group和Sum的应用
- Linq中Group和Sum的应用
- Linq group by 的妙用
- Linq To List Group 和Sum的联合 用法
- group by 的 sum 求和
- 有sum,没有group by的Sql用Linq怎么写
- linq中的group by 和 left join
- linq group by Having 语句的写法.
- linq group by
- linq group by
- Linq group by 练习
- linq group by 处理
- linq-group by
- 一个sql的四种写法:sum...case.when、sum if、select(select..)、group by
- group by,having,sum的使用例子
- Spring MVC静态文件处理
- 一个Linux下C线程池的实现(转)
- 入驻CDSN博客
- STM32唯一的ID
- opencv中要用release编译
- 一个Linq Group By 和Sum的范例
- 关于POI对Excel2007中图片的处理,获取图片在excel中对应的位置
- Flex 如何在事件响应时传值
- javascript:window.close()在chrome,Firefox下失效问题解决方法
- [leetcode]Subsets
- ActiveMQ-cpp客户端程序应用异常退出问题
- 计算书费
- UVA128 - Software CRC(大数取模)
- Cisco 3750交换机配置无故丢失,恢复一例