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) 如果要查看本类 ...
随机推荐
- bing 输入法,切换简体、繁体快捷键与myeclipse 格式化代码冲突。。
bing 输入法,切换简体.繁体快捷键与myeclipse 格式化代码冲突...蛋碎 myeclipse 代码格式化快捷键是:ctrl+shift+F,bing输入法简体.繁体切换也是,于是蛋疼的事情 ...
- C. Ilya and Sticks
C. Ilya and Sticks time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Joynet示例:知乎爬虫(搜索关键字相关回答,并下载其中的---图(mei)片(nv))
先贴爬虫程序下载地址:http://pan.baidu.com/s/1c2lxl1e 下载解压后 可直接运行 其中的 run.bat:或者你手动打开命令行执行:Joynet examples\Spid ...
- vb.net机房收费系统之组合查询
我个人一直认为,组合查询是机房收费系统的一个难点,尤其是用到三层之后,如果要为组合查询中的每一个查询建立一个显然是太麻烦了. 下面介绍一下我的方法,对大家起个参考作用. 我将该表中可输入的内容定义为一 ...
- 放飞App:移动产品经理实战指南
<放飞App:移动产品经理实战指南> 基本信息 原书名:App savvy:rurning ideas into iPhone and iPad Apps customers really ...
- 使用aop记录数据库操作的执行时间
在项目中,我们往往需要记录数据库操作的时间,根据操作时间的不同,分别记录不同等级的日志. 首先我们可以写一个类实现MethodInterceptor接口: import org.aopalliance ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- 【转】唱吧CEO陈华:创业四年,我积累的7点管理经验
现象级产品“唱吧”至今拥有令人羡慕的用户数量,3亿.而这一切,却用了短短不到四年时间.唱吧团队如何应对越来越复杂的市场变化:怎样用人,才能不断激励新老员工做出更棒的业绩:CEO陈华又如何用“下大雪”模 ...
- IOS 支付宝、微信回调传值给H5网页
这里用是的苹果原生和JS的交互 .有不明白JavaScriptCore框架的可以去网上搜索下这方面的资料很多废话不多说直接上代码 @protocol JSContextDelegate <JSE ...
- 判断checked是否选中
if($('#checkbox-id').is(':checked')) { // do something }