0

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. 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. 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. 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);

enter image description here

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.

enter image description here

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.

Caesar Tex
  • 295
  • 1
  • 5
  • 18
  • 1
    post the whole code please – Stormhashe Jun 04 '19 at 20:56
  • I added some more code. can't post the original since it's confidential – Caesar Tex Jun 04 '19 at 21:02
  • 1
    Please show some sample data and what you expect the outcome to be, in a [mcve]. Note that you do not need to post your actual code (in fact you shouldn't, as it most likely includes a lot of irrelevant details); just enough sample code so that others can reproduce the issue you're having. – Heretic Monkey Jun 04 '19 at 21:08
  • Are you trying to run a conditional test over the key results? For example, if your results are { key1: 1, key2: 5} are you trying to run a test to filter those results down further? I'm not sure how you want to group and then group again (there is only one key to group on), but you can filter the results down with another Where clause put after the group like here: https://stackoverflow.com/questions/2078736/linq-with-group-by-having-count Basically, you can add additional .Where or .Select after your .Groupby to filter the results by the key or by the result count, as needed. – HBlackorby Jun 04 '19 at 21:10
  • I expect you want something like [Nested Group by LINQ](https://stackoverflow.com/q/9578887/215552). – Heretic Monkey Jun 04 '19 at 21:12
  • I added an image. let me know if that helps. The result view contains another 20 or so results for this particular key. I need to group by on some key within the resultview. – Caesar Tex Jun 04 '19 at 21:25

1 Answers1

1

In a nutshell,

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

var result = q.Groupby(x => x.a).Select(g => g.Groupby(x => x.c));
Samuel Vidal
  • 883
  • 5
  • 16