0

I know that if I need to group in Linq I do this:

var results = from p in persons
              group p by p.PersonID into g

and if I need to group using another field/criteria, I do this:

var results2 = from p in persons
              group p by p.PersonName into g

but this means traversing the list I have twice. Is there anyway to do both grouping in one statement?

Hasan Shouman
  • 2,162
  • 1
  • 20
  • 26
  • 1
    Does this answer your question? [Group By Multiple Columns](https://stackoverflow.com/questions/847066/group-by-multiple-columns) – Eldho May 24 '21 at 15:46

1 Answers1

2

Grouped by key is person_id and person_name:

var results = from p in persons
              group p by new { p.PersonID , p.PersonName } into g
dev1
  • 171
  • 1
  • 5
  • This will group by both columns, what I need is to group by two different criteria, to get the count of each separately – Hasan Shouman May 25 '21 at 07:55