using System;using System.Linq;namespace ConsoleApplication1{ class Program { public class S { public int Year; public int Month; public int Day; } static void Main(string[] args) { var m = new []{ new S{Year = 2000, Month = 1, Day = 10}, new S{Year = 2000, Month = 2, Day = 10}, new S{Year = 2010, Month = 1, Day = 1}, new S{Year = 2010, Month = 2, Day = 1}, new S{Year = 2010, Month = 1, Day = 2}, new S{Year = 2010, Month = 2, Day = 2}, new S{Year = 2000, Month = 1, Day = 2}, new S{Year = 2000, Month = 2, Day = 2}, }; var q2 = from s in m group s by s.Year into YearGroup select new { Year = YearGroup.Key, MonthGroups = from s2 in YearGroup group s2 by s2.Month into MonthGroup select new { Month = MonthGroup.Key, Days = from s3 in MonthGroup orderby s3.Day select s3.Day } }; var q = m.GroupBy( s => s.Year, (Year, YearGroup) => new { Year, MonthGroups = YearGroup.GroupBy( s2 => s2.Month, (Month, MonthGroup) => new { Month, Days = MonthGroup.OrderBy(s3 => s3.Day).Select(s3 => s3.Day) } ) } ); foreach (var elem in q) //foreach (var elem in q2) { Console.WriteLine("Year = {0}", elem.Year); foreach (var elem2 in elem.MonthGroups) { Console.WriteLine("\tMonth = {0}", elem2.Month); foreach (var day in elem2.Days) Console.WriteLine("\t\tDay = {0}", day); } } } }}//Year = 2000// Month = 1// Day = 2// Day = 10// Month = 2// Day = 2// Day = 10//Year = 2010// Month = 1// Day = 1// Day = 2// Month = 2// Day = 1// Day = 2 详细解决方案
LINQ技巧:怎么通过多次调用GroupBy实现分组嵌套
热度:53 发布时间:2016-04-29 08:12:14.0
相关解决方案
- LINQ to sQL:业务层多个Class Library,or 只用一个?该如何解决
- magaView.HasMonth = db.Library.GroupBy(a => a.TimeBook.Month).Select(a => a.Key),该怎么处理
- Linq 初学,问个有关问题.
- Linq To Sql的有关问题(关于同步更新)
- Linq 求转换。
- linq 分组求和解决思路
- linq 中如何表达 SQL中的 not in
- Linq 有结果就结贴一小时看一次贴子解决思路
- Linq to Sql中编译Linq查询有关问题
- LINQ 返回 datatable的有关问题
- linq 中 g.first()怎么跟g.count() 连用啊
- 命名空间“System.Data”中不存在门类或命名空间名称“Linq”(是否缺少程序集引用?)
- asp.net linq to sql 有关问题
- 如何引用System.linq
- 求教sql 转 linq to ef 的有关问题,高手帮帮忙.
- "Linq to object"实现以及异常排除
- LinQ
- mvc + linq 的有关问题非常简单
- linq 操作 datatable指定的转换无效解决方案
- LINQ 好使吗?该如何处理
- Linq to Entities 一对多外键关联 添加数据报错?该怎么解决
- linq 排名,该怎么解决
- “RIA Services+linq to sql”和“Web中DAL层”解决方案
- linq ForEach方法如何使用
- MVC+Flexigrid+Linq to sql数据 显示,增加,删除,修改,该怎么处理
- “LINQ”提供“Service”服务的疑问解决办法
- 求一本asp.net学习用书,小弟我现在有一本asp.net 3.5揭秘了,想学学ajax,缓存,linq,自定义控件,sqlhelper等
- 为什么老是自动添加using System.Linq和using System.Xml.Linq,该怎么解决
- linq in有关问题
- 关于 Linq 的话语