LINQ 嵌套组 [英] Nested Group by LINQ

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

问题描述

我无法用 LINQ 查询解决这个问题.

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

所以我们的表结构如下:编号 ||bug_category ||错误名称 ||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 = AUDIO :: 错误数 --> 严重 = 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:

(其中 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()
            };

那么:

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

或者(但见下文):

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天全站免登陆