通过LINQ嵌套组 [英] Nested Group by LINQ

查看:107
本文介绍了通过LINQ嵌套组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法与LINQ查询来解决这个问题。

I am unable to solve this problem with the LINQ Query.

因此,我们有表的结构如下:
标识|| bug_category || bug_name || bug_details || bug_priority

So we have the table structure as follows: Id || bug_category || bug_name || bug_details || bug_priority

我想通过bug_category第一集团。对于每个bug_category,我想反过来组由bug__priority

I want to group by bug_category first. For each bug_category, I want to in turn group by bug__priority.

所以基本上我想是这样的:

So basically I want something like :

bug_category =音频::没有错误的 - >临界值= 3,中等= 2,低= 7的错误结果
bug_category = VIDEO ::没有错误的 - >临界值= 5,中= 1,低= 9错误

bug_category = AUDIO :: No of BUGS --> Critical = 3, Medium = 2 and Low = 7 bugs.
bug_category = VIDEO :: No of BUGS --> Critical = 5, Medium = 1 and Low = 9 bugs.

下面的查询返回的类别和customer_priority的所有独特的组合:

The below query returns all unique combinations of category AND customer_priority:

(其中< STRONG> RawDataList 简直就是一个具有上述结构的数据列表)

(where RawDataList is simply a List of data which has the above mentioned structure )

        var ProceesedData = from d in RawDataList
                      group d by new { d.bug_category, d.bug_priority } into g
                      select new
                      {
                          g.Key.bug_category,
                          g.Key.bug_priority
                      };



下面的查询返回的类别之后的该类别记录的列表:

The below query returns the category followed by a list of records in that category:

            var ProceesedData = from d in RawDataList
                      group d by d.bug_category into g
                      select new { g.Key, records = g
                      };



不过,我不能为ProcessedData(返回变量)进一步进行类型未知。任何对此的思考

But I am unable to proceed further as ProcessedData(the return variable) is an unknown type. Any thoughts on this?

推荐答案

我怀疑你想要的(名称更改为更地道):

I suspect you want (names changed to be more idiomatic):

var query = from bug in RawListData
            group bug by new { bug.Category, bug.Priority } into grouped
            select new { 
                Category = grouped.Key.Category,
                Priority = grouped.Key.Priority,
                Count = grouped.Count()
            };



然后:

Then:

foreach (var result in query)
{
    Console.WriteLine("{0} - {1} - {2}",
                      result.Category, result.Priority, result.Count);
}



另外的(但见后):

Alternatively (but see later):

var query = from bug in RawListData
            group bug by new bug.Category into grouped
            select new { 
                Category = grouped.Category,
                Counts = from bug in grouped
                         group bug by grouped.Priority into g2
                         select new { Priority = g2.Key, Count = g2.Count() }
            };

foreach (var result in query)
{
    Console.WriteLine("{0}: ", result.Category);
    foreach (var subresult in result.Counts)
    {
        Console.WriteLine("  {0}: {1}", subresult.Priority, subresult.Count);
    }
}



编辑:正如在评论中指出,这将导致多个SQL查询。要获得类似的结果结构,但更有效的,你可以使用:

As noted in comments, this will result in multiple SQL queries. To obtain a similar result structure but more efficiently you could use:

var dbQuery = from bug in RawListData
              group bug by new { bug.Category, bug.Priority } into grouped
              select new { 
                  Category = grouped.Key.Category,
                  Priority = grouped.Key.Priority,
                  Count = grouped.Count()
              };

var query = dbQuery.ToLookup(result => result.Category,
                             result => new { result.Priority, result.Count };


foreach (var result in query)
{
    Console.WriteLine("{0}: ", result.Key);
    foreach (var subresult in result)
    {
        Console.WriteLine("  {0}: {1}", subresult.Priority, subresult.Count);
    }
}

这篇关于通过LINQ嵌套组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆