LINQ技巧:如何通过多次调用GroupBy实现分组嵌套

问题如上,解决如下,目标在最下面:结果:
using System;
using System.Linq;
using System.Collections.Generic; namespace ConsoleApplication1
{
class Program
{
public class Sdata
{
public string gather;
public int shotcount; } static void Main(string[] args)
{
var m = new[]{
new Sdata{gather = "",shotcount = },
new Sdata{gather = "", shotcount =},
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
new Sdata{gather = "", shotcount = },
}; var q2 =
from s in m
group s by s.gather into gatherGroup
select new
{
gather = gatherGroup.Key,
shotcountGroups =
from s2 in gatherGroup
group s2 by s2.shotcount into shotcountGroups
select new
{
shotcount = shotcountGroups.Key,
//Days =
// from s3 in shotcountGroups
// orderby s3.Day
// select s3.Day
}
}; foreach(var item in q2)
{
Console.WriteLine("1gather={0}",item.gather);
foreach(var itme2 in item.shotcountGroups)
{
Console.WriteLine("\tshotcount = {0}",itme2.shotcount);
}
} var q = m.GroupBy(
s => s.gather,
(gather, gatherGroup) => new
{
gather,
shotcountGroups =
gatherGroup.GroupBy(
s2 => s2.shotcount,
(shotcount, shotcountGroups) => new
{
shotcount,
//Days = shotcountGroups.OrderBy(s3 => s3.Day).Select(s3 => s3.Day)
}
)
}
); List<object> listobj = new List<object>();
List<object> obj = new List<object>();
foreach (var elem in q)
//foreach (var elem in q2)
{
int tc = elem.shotcountGroups.Count();
var d=new List<object>() ; Console.WriteLine("2gather = {0}", elem.gather);
d.Add(elem.gather);
foreach (var elem2 in elem.shotcountGroups)
{
Console.WriteLine("\tshotcount = {0}", elem2.shotcount);
//foreach (var day in elem2.Days)
// Console.WriteLine("\t\tDay = {0}", day);
d.Add(elem2.shotcount);
}
Console.WriteLine("eachobj is {0}",d.Count());
listobj.Add(d);
}
Console.WriteLine("listobj is {0}", listobj.Count); foreach (var mdata in listobj)
{
Console.WriteLine("listobj is {0}", listobj.ToList());
} Console.Read();
}
}
}
gather = 100002
shotcount = 28
shotcount = 44
gather = 100003
shotcount = 8
shotcount = 20
gather = 100004
shotcount = 55
shotcount = 60
gather = 100005
shotcount = 10
shotcount = 60
gather = 100006
shotcount = 24
shotcount = 75
gather = 100010
shotcount = 41
shotcount = 81
gather = 100012
shotcount = 75
shotcount = 100
LINQ技巧:如何通过多次调用GroupBy实现分组嵌套的更多相关文章
- c# 通过GroupBy 进行分组
有时候我们需要数据根据一些字段进行分组,这时候用orderBy很方便.不多说了.直接上代码: class Ma { public int number { get; set; } public str ...
- C# Linq及Lamda表达式实战应用之 GroupBy 分组统计
在项目中做统计图表的时候,需要对查询出来的列表数据进行分组统计,首先想到的是避免频繁去操作数据库可以使用 Linq eg: //例如对列表中的Cu元素进行按年GroupBy分组统计 //包含年份,平均 ...
- C#_技巧:.net下C++调用C#的dll
C#编译一个dll,比如命名空间为Csharp,里面有个类A,字段x,产生一个Csharp.dll C++ 配置,让C++支持CLR C++调用方法: #include <iostream> ...
- Ruby操作VBA的注意事项和技巧(2):宏里调用和控制窗体以及窗体上的控件、不同workbook之间的宏互相调用
4.宏里调用并控制窗体以及窗体上的各种控件 1 Sub Criterion_Check() 2 If Workbooks.count = 0 Then '如果当前没有打开的工作薄的话需要发出警告 3 ...
- c# Linq及Lamda表达式应用经验之 GroupBy 分组
示例1: GroupBy 分组在List<>泛型中的应用 原表: 按姓名Nam 分组后结果: 对DATATABLE 进行LAMDA查询时必须在项目的引用中添加 System.Data.Da ...
- (转)c# Linq及Lamda表达式应用经验之 GroupBy 分组
本文转载自:http://www.cnblogs.com/han1982/p/4138163.html 示例1: GroupBy 分组在List<>泛型中的应用 原表: 按姓名Nam 分组 ...
- Linq技巧4——怎么在.NET 3.5 SP1中伪造一个外键属性
在.NET 4.0 的EF 中,增加了FK Associations 的功能,但是在.NET 3.5 SP1 中,仅仅支持独立的关联,这意味着FK 栏位不能作为实体的属性来使用,也就是说在使用的时候, ...
- Linq技巧1——关联实体查询排序
假如想查询拖欠按揭超过30天的银行帐号,同时查询出他们的单据,并且需要按照单据日期进行排序,这样可以首先看到最近的单据,方便找出问题. 大多数人都知道EF可以使用Include()热加载关系实体,例如 ...
- Eclipse 使用技巧之 ---- 查看本类调用和被调用列表
当工程复杂的情况下,用眼睛去人工查看调用情况是很费力也没必要的.我们需要用 Eclipse 来做这点. (1) 我们查看本类调用他类情况可以直接看 import . (2) 如果要查看本类 ...
随机推荐
- 如何使用NSOperations和NSOperationQueues(二)
"每一个应用程序至少有一个主线程.线程的工作就是去执行一系列的指令.在Cocoa Touch中,主线程包含应用程序的主运行回路.几乎所有你写的代码都会在主线程中执行,除非你特别创建" ...
- C#实现插件式架构的方法
插件式架构,一种全新的.开放性的.高扩展性的架构体系.插件式架构设计近年来非常流行,基于插件的设计好处很多,把扩展功能从框架中剥离出来,降低了框架的复杂度,让框架更容易实现.扩展功能与框架以一种很松的 ...
- Centos7 安装redis3.2.3 过程
1:安装wget: yum install wget2:安装pip: 1:sudo yum -y install epel-release 2:sudo yum -y install py ...
- 剑指Offer04 重建二叉树
代码有问题 /************************************************************************* > File Name: 04_ ...
- 第五节 关于SpringMVC中Ajax的配置和应用[下午]
成熟,不是学会表达,而是学会咽下,当你一点一点学会克制住很多东西,才能驾驭好人生. 还有一周,祥云19就算结算了,一个半月的相处希望,胖先生算一个合格的老师 小白,小蔡,2婷婷,小猴,小恒,小崔,小龙 ...
- html+CSS--水平居中设置(定宽块状元素)
来源:http://www.imooc.com/code/4336 当被设置元素为 块状元素 时用 text-align:center 就不起作用了,这时也分两种情况:定宽块状元素和不定宽块状元素. ...
- javascript-函数的参数和return语句
× 目录 [1]参数 [2]Arguments对象 [3]函数重载 [4]return 语句 ------------------------------------- 一.参数(最多25个) 可以动 ...
- JQuery.Gantt(甘特图)开发
一.简介 JQuery.Gantt是一个开源的基于JQuery库的用于实现甘特图效果的可扩展功能的JS组件库. 二.前端页面 2.1 资源引用 首先需要将下载到的源码中的CSS.IMG.JS等资源放入 ...
- Session,Cookie,jsessionid,Url重写
在一些投票之类的场合,我们往往因为公平的原则要求每人只能投一票,在一些WEB开发中也有类似的情况,这时候我们通常会使用COOKIE来实现,例如如下的代码: < % cookie[]cookies ...
- 使用OLEDB读取excel和csv文件
这是我第一次在博客上写东西,简单的为大家分享一个oledb读取文件的功能吧,这两天在做一个文件导入数据库的小demo,就想着导入前先在页面上展示一下,之前调用Microsoft.Office.Inte ...