简体   繁体   English

如何按LINQ分组结果分组

[英]How to group by on result of LINQ group by

I'm trying to group by on result of group by. 我正在尝试根据分组结果分组。 I need to group by on column "a" of result q from a dapper query and then I need to further group by on result of this grouping. 我需要根据精简查询的结果q的“ a”列进行分组,然后再根据此分组的结果进行分组。 How can I do this or how do I access the "key' and its elements of each grouped by value? 如何执行此操作,或者如何访问按值分组的每个“键”及其元素?

The first groupby returns "GroupedEnumerable" of my class. 第一个groupby返回类的“ GroupedEnumerable”。 There are two elements in it (since there are only two unique values for column a but that can change) and then each element has a key and then Result. 其中有两个元素(因为a列只有两个唯一值,但是可以更改),然后每个元素都有一个键,然后有一个Result。 I need to further group by some value within the result of each key. 我需要进一步根据每个键的结果中的一些值进行分组。

var result = q.Groupby(x => x.a);

Let's say the Model/Class looks like this 假设模型/类看起来像这样

public class MyClass
{
    public string a { get; set; }
    public string b { get; set; }
    public string c { get; set; }
    public string d { get; set; }
    public string e { get; set; }


}

I get a list of objects from a dapper query. 我从小巧的查询中获取对象列表。

var v = repository.getResult(id);

Now I group by on a. 现在我按一个分组。

 var t= v.Groupby(x => x.a);

在此处输入图片说明

Now in resultview there are multiple elements and each element contains some properties of original class like (a,b,c,d, and so on) I need to group on say c now. 现在在resultview中有多个元素,每个元素都包含原始类的某些属性,例如(a,b,c,d等),我现在需要对c进行分组。

在此处输入图片说明

I guess I'm just interested in knowing what kind of result set does this result in. I don't have to use linq, I can just use good old loop if I knew how to get to inner elements. 我想我只是想知道该结果会导致哪种结果集。我不必使用linq,如果我知道如何使用内部元素,则可以使用旧的循环。

简而言之,

var result = q.Groupby(x => new { x.a, x.c });

var result = q.Groupby(x => x.a).Select(g => g.Groupby(x => x.c));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM