String Concatenation in GroupBy

I am doing GroupBy with Linq on IQueryable . But I would like to make GroupBy in a concatenated string. Now I have the following:

 SomeList.GroupBy(x => x.Code + x.Location) 

This works for me, however it also changed x.Code to a concatenated string x.Code + x.Location .

Is there a way to temporarily combine variables in GroupBy , something like:

 SomeList.GroupBy(x => (x.Code + x.Location) as TempVar) 
+4
source share
2 answers

Grouping by each property individually will lead to the same as the combined grouping.

 SomeList.GroupBy(x => new {x.Code, x.Location}); 
+2
source

To answer your specific question

Is there a way to temporarily combine variables in GroupBy ...

 someList.GroupBy(x => new { TempConcat = x.Code + x.Location }) 

However, I believe the NinjaNye answer is better for what you want, not what you asked for :) .

0
source

Source: https://habr.com/ru/post/1494249/


All Articles