问题如上,解决如下,目标在最下面:结果: 

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实现分组嵌套的更多相关文章

  1. c# 通过GroupBy 进行分组

    有时候我们需要数据根据一些字段进行分组,这时候用orderBy很方便.不多说了.直接上代码: class Ma { public int number { get; set; } public str ...

  2. C# Linq及Lamda表达式实战应用之 GroupBy 分组统计

    在项目中做统计图表的时候,需要对查询出来的列表数据进行分组统计,首先想到的是避免频繁去操作数据库可以使用 Linq eg: //例如对列表中的Cu元素进行按年GroupBy分组统计 //包含年份,平均 ...

  3. C#_技巧:.net下C++调用C#的dll

    C#编译一个dll,比如命名空间为Csharp,里面有个类A,字段x,产生一个Csharp.dll C++ 配置,让C++支持CLR C++调用方法: #include <iostream> ...

  4. Ruby操作VBA的注意事项和技巧(2):宏里调用和控制窗体以及窗体上的控件、不同workbook之间的宏互相调用

    4.宏里调用并控制窗体以及窗体上的各种控件 1 Sub Criterion_Check() 2 If Workbooks.count = 0 Then '如果当前没有打开的工作薄的话需要发出警告 3 ...

  5. c# Linq及Lamda表达式应用经验之 GroupBy 分组

    示例1: GroupBy 分组在List<>泛型中的应用 原表: 按姓名Nam 分组后结果: 对DATATABLE 进行LAMDA查询时必须在项目的引用中添加 System.Data.Da ...

  6. (转)c# Linq及Lamda表达式应用经验之 GroupBy 分组

    本文转载自:http://www.cnblogs.com/han1982/p/4138163.html 示例1: GroupBy 分组在List<>泛型中的应用 原表: 按姓名Nam 分组 ...

  7. Linq技巧4——怎么在.NET 3.5 SP1中伪造一个外键属性

    在.NET 4.0 的EF 中,增加了FK Associations 的功能,但是在.NET 3.5 SP1 中,仅仅支持独立的关联,这意味着FK 栏位不能作为实体的属性来使用,也就是说在使用的时候, ...

  8. Linq技巧1——关联实体查询排序

    假如想查询拖欠按揭超过30天的银行帐号,同时查询出他们的单据,并且需要按照单据日期进行排序,这样可以首先看到最近的单据,方便找出问题. 大多数人都知道EF可以使用Include()热加载关系实体,例如 ...

  9. Eclipse 使用技巧之 ---- 查看本类调用和被调用列表

    当工程复杂的情况下,用眼睛去人工查看调用情况是很费力也没必要的.我们需要用 Eclipse 来做这点. (1)    我们查看本类调用他类情况可以直接看 import . (2)    如果要查看本类 ...

随机推荐

  1. hadoop2 作业执行过程之yarn调度执行

    YARN是hadoop系统上的资源统一管理平台,其主要作用是实现集群资源的统一管理和调度(目前还不完善,只支持粗粒度的CPU和内存的的调配): 它的基本思想是将Mapreduce的jobtracker ...

  2. 一个对称加密、解密的方法C#工具类

    封装了一个对称加解密的类,用私钥和密钥加解密 using System; using System.Collections.Generic; using System.Text; using Syst ...

  3. [Arduino] Arduino Uno R3 中文介绍

    Arduino UNO是Arduino USB接口系列的最新版本,作为Arduino平台的参考标准模板.UNO的处理器核心是ATmega328,同时具有14路数字输入/输出口(其中6路可作为PWM输出 ...

  4. 【Shell脚本学习10】Shell运算符:Shell算数运算符、关系运算符、布尔运算符、字符串运算符等

    Bash 支持很多运算符,包括算数运算符.关系运算符.布尔运算符.字符串运算符和文件测试运算符. 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最 ...

  5. (转)内网网站发布到外网-nat123动态公网IP动态域名解析

    环境描述: 路由器分配的是动态公网IP,且有路由器登录管理权限,网站服务器部署在路由器内部网络.如何将内网网站发布到外网大众访问? 解决方案: 内网使用nat123动态域名解析,将域名实时固定解析到路 ...

  6. DATASNAP REST WEBSERVICES中间件如何跨平台使用

    DATASNAP REST WEBSERVICES中间件如何跨平台使用   准备使用DELPHI开发移动设备开发的朋友对DATASNAP REST中间件不可不了解. DATASNAP REST新型WE ...

  7. JQuery Mobile 实战一

    今天我们来使用JQuery Mobile来开发一个web mobile app. 要实现的如下所示效果: 开始: 第一步:添加JS包等引用,直接去官网下载最新的JQuery Mobile 包,http ...

  8. MySql运算符

    算数运算符 加(+).减(-).乘(*).除(/).求余(%) 比较运算符 大于(>).小于(<).等于(=).大于等于(>=).小于等于(<=).不等于(!= 或者 < ...

  9. c#获取网页内容的三种方法

    1.webclient WebClient wc = new WebClient(); Stream stm = wc.OpenRead(str_url); StreamReader sr = new ...

  10. 研究validation插件到现在的感受

    1.比较累 2.看了几十个页面参考是有的,要抓住问题的实质,实质在于要改插件代码.因为它本身不提供这个方法. 3.对了,还没有描述这个问题,问题就是再次验证时,成功的样式不消失.解决方法如下: 修改了 ...