I assume that you can have more than three columns from the data2 field?
If this is not the case, you can request a query that returns an anonymous type with a variable number of properties. You need to return an array or some list for a set of data2 values.
I think this is what you can do:
var query = from mt in MyTable group mt.data2 by mt.data1 into gmts let d2 = gmts.ToArray() select new { data1 = gmts.Key, data2 = d2, length = d2.Length, }; var pending = query.ToArray(); var maxLength = pending.Max(p => p.length); Func<string[], string[]> extend = xs => { var r = new string[maxLength]; xs.CopyTo(r, 0); return r; }; var results = from p in pending select new { p.data1, data2 = extend(p.data2), };
This creates a series of anonymous types, with the data2 array data2 same size to match the maximum number of results for any of the data1 fields.
The query is still executed as a single SQL query. And in-memory processing is fast.
Does this work for you?
EDIT
Since you know that you have a fixed number of columns (according to the comment), you can easily modify my results query to meet your requirements:
var results = from p in pending let d2s = extend(p.data2) select new { p.data1, first = d2s[0], second = d2s[1], third = d2s[2], };
Enigmativity
source share