Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert IEnumerable<IGrouping<,>> to array

Tags:

c#

linq

Is there an easy way of converting remaining in the following code to a 1-D array.

var groups = data.OrderBy(d => d.Time).GroupBy(d => d.Period);
var first = groups.First().ToArray();
var remaining = groups.Skip(1).??
like image 912
kasperhj Avatar asked Jun 29 '26 16:06

kasperhj


2 Answers

var remaining = groups.Skip(1).SelectMany(g=>g).ToArray();
like image 86
King King Avatar answered Jul 01 '26 06:07

King King


Use SelectMany to "flatten" a collection of collections:

var remaining = groups.Skip(1).SelectMany(d => d).ToArray();
like image 37
D Stanley Avatar answered Jul 01 '26 05:07

D Stanley