How to concat to IEnumerable IGrouping?

533 Views Asked by At

I have two IEnumerable IGrouping in my C# program:

IEnumerable<IGrouping<string, FileItem>> number1
IEnumerable<IGrouping<string, FileItem>> number2

Is it somehow possible to concat these two IEnumerable into one?

4

There are 4 best solutions below

0
On BEST ANSWER

You could make use of the Concat method:

var concatenated = number1.Concat(number2);

For more info about this method, please have a look here.

0
On

Concat should work:

IEnumerable<IGrouping<string, FileItem>> concatenated = number1.Concat(number2);

https://msdn.microsoft.com/en-us/library/bb302894(v=vs.110).aspx

0
On

Are you looking for something like that;

var groupedNumbers = number1.Concat(number2);
0
On

This doesn't merge groups, but only concat the enumeration, with keys duplicated. The following code illustrate the issue:

var mergedGroups = firstGroups .Concat ( secondGroups );

var duplicateKeys = mergedGroups .GroupBy(group => group.Key) .Where(group => group.Count() > 1) .Select(group => group.Key) .ToList();