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

Posted GoneWithWind

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = "100002",shotcount = 28},
                new Sdata{gather = "100002", shotcount =44},
                new Sdata{gather = "100003", shotcount = 8},
                new Sdata{gather = "100003", shotcount = 20},
                new Sdata{gather = "100004", shotcount = 55},
                new Sdata{gather = "100004", shotcount = 60},
                new Sdata{gather = "100005", shotcount = 10},
                new Sdata{gather = "100005", shotcount = 60},
                new Sdata{gather = "100006", shotcount = 24},
                new Sdata{gather = "100006", shotcount = 75},
                new Sdata{gather = "100010", shotcount = 41},
                new Sdata{gather = "100010", shotcount = 81},
                new Sdata{gather = "100012", shotcount = 75},
                new Sdata{gather = "100012", shotcount = 100},
            };

            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实现分组嵌套的主要内容,如果未能解决你的问题,请参考以下文章

atitit. groupby linq的实现-----linq框架选型 java .net php

lua如何实现 静态变量,多次调用同一个函数时,只初始化一次指定的变量值 没什么分,谢谢帮忙

javaScript设计模式简记-技巧型设计模式

javaScript设计模式简记-技巧型设计模式

csharp 对排序输入没有真正有效的LINQ GroupBy()实现

Linq通过GroupBy分组统计对象出现次数