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) 如果要查看本类 ...
随机推荐
- iOS H5 容器的一些探究(一):UIWebView 和 WKWebView 的比较和选择
来源:景铭巴巴 链接:http://www.jianshu.com/p/84a6b1ac974a 一.Native开发中为什么需要H5容器 Native开发原生应用是手机操作系统厂商(目前主要是苹果的 ...
- Python笔记(一)
我是一名211高校软件工程大三学生,由于前段时间一直在找实习公司.笔试面试了很多公司,虽然有一定的基础,但是还是被某些公司面试官像虐狗一样的虐了.最后找到了一个口碑比较好的外企,主攻信息安全方面.这段 ...
- C# 递归程序 获取某个节点下的全部子节点
/// <summary> /// 获取组织结构树 /// </summary> /// <param name="list"></par ...
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler
mybatis 比 ibatis 改进了很多,特别是支持了注解,支持了plugin inteceptor,也给开发者带来了更多的灵活性,相比其他ORM,我还是挺喜欢mybatis的. 闲言碎语不要讲, ...
- java正则表达式常用实例——借鉴思路
转载自:http://mp.weixin.qq.com/s?__biz=MjM5OTM4NDMyMg==&mid=2650044497&idx=1&sn=dc80fa35f7e ...
- Google IP 最新地址
原文地址:https://ideas.spkcn.com/technology/250.html 2015年 目前最新可以直接访问google的IP91.213.30.152173.194.77.14 ...
- 日期字符串转换 and 两个日期相减
//创建当前日期 NSDate *date = [NSDate date]; //创建日期格式类 NSDateFormatter *dateFormat = [[NSDateFormatter all ...
- Oracle数据库作业-2 添加主键 外键
一.在表student中添加主键sno
- dom操作中的js优化
频繁地对于DOM进行操作的很是损耗性能,但在富网页应用中我们编写脚本无可避免地要跟DOM打交道,到底怎么才能优化这个性能瓶颈呢,大致从以下三种情况去考虑: 访问和修改DOM元素 修改DOM样式,会造成 ...
- 使用secureCRT连接VMware-Ubuntukylin虚拟机
使用SecureCRT连接VMware时总是提醒主机拒绝连接.这时可以使用sudo apt-get install openssh-server openssh-client,在主机上安装ssh. 安 ...