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

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. python编码encode和decode

    计算机里面,编码方法有很多种,英文的一般用ascii,而中文有unicode,utf-8,gbk,utf-16等等. unicode是 utf-8,gbk,utf-16这些的父编码,这些子编码都能转换 ...

  2. NSNotificationCenter 使用姿势详解

    来源:JamesYu 链接:http://www.jianshu.com/p/a4d519e4e0d5 最近在做平板的过程中,发现了一些很不规范的代码.偶然修复支付bug的时候,看到其他项目代码,使用 ...

  3. Bus Pass

    ZOJ Problem Set - 2913 Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot b ...

  4. Spring mvc编码配置

    Spring3 MVC也带有自己的编码: jar包:org.springframework.web-3.0.0.RELEASE.jar 只需要在web.xml配置即可: <!-- spring ...

  5. solr返回的字段带有中括号问题

    在solr返回的结果集的content字段返回格式为: "content": ["测试content" ], 经过对比title 和content字段,发现co ...

  6. 数据结构【三】:简单优先队列PriorityQueue

    在数据结构[二]:简单阻塞队列BlockingQueue的基础上添加权限属性:priority,并控制enqueue时根据priority排序插入. 1.定义priority取值范围0~9 2.deq ...

  7. 【Mood-10】每个程序员都应该读的30本书

    “如果能时光倒流,回到过去,作为一个开发人员,你可以告诉自己在职业生涯初期应该读一本,你会选择哪本书呢?我希望这个书单列表内容丰富,可以涵盖很多东西.” 很多程序员响应,他们在推荐时也写下自己的评语. ...

  8. 有一种风格,叫做 Low Poly 3D

    原作:Simon阿文    杂交编辑者:RhinoC       个人更推崇使用第二款神器 ImageTriangulator :http://www.conceptfarm.ca/2013/port ...

  9. IDG合伙人李丰:O2O中的C2C蕴藏巨大商机

    [ 亿欧导读 ] IDG合伙人李丰表示,每个新趋势出现,都是在解决上一轮行业革新时没有解决好的市场需求.而O2O中的C2C将会出现巨大商机的原因在于移动互联网的出现创造了新的交互方式,可以更快速的匹配 ...

  10. latex之设置字体大小

    \tiny\scriptsize\footnotesize\small\normalsize\large\Large\LARGE\huge\Huge