Given the following table, as an example:
+----+---------+-----------+ | ID | GroupID | OtherData | +----+---------+-----------+ | 1 | 1 | w4ij6u | +----+---------+-----------+ | 2 | 2 | ai6465 | +----+---------+-----------+ | 3 | 2 | ows64rg | +----+---------+-----------+ | 4 | 2 | wqoi46suj | +----+---------+-----------+ | 5 | 3 | w9rthzv | +----+---------+-----------+ | 6 | 3 | 03ehsat | +----+---------+-----------+ | 7 | 4 | w469ia | +----+---------+-----------+ | 8 | 5 | nhwh57rt | +----+---------+-----------+ | 9 | 5 | mwitgjhx4 | +----+---------+-----------+
How can I efficiently get a List<List<DataRow>> extracted from this table that is based upon the “GroupID” column?
Basically, I want the result to be:
MyList(0) = List: 1 DataRow, ID(s) 1
MyList(1) = List: 3 DataRows, ID(s) 2,3,4
MyList(2) = List: 2 DataRows, ID(s) 5,6
MyList(3) = List: 1 DataRow, ID(s) 7
MyList(4) = List: 2 DataRows, ID(s) 8,9
Here’s the problem though: This DataTable contains hundreds of columns and tens of thousands of rows, so this operation must be as efficient as possible.
I have already tried the following methods:
- Creating a
DataViewwith a Row Filter, and extracting a table/list of rows from that view. - A Linq query within a loop, after getting a unique list of GroupIDs. The Linq query selects each “set” of GroupIDs based on a
Whereclause.
I’m hoping someone else has a better, more efficient way of extracting this data.
Turns out that A) My returning data set was too large (because of a bug), and B) LINQ is probably the fastest way to do this, without writing some very long or hack-ish code. Thanks for your ideas, everyone, but I’ll stick with LINQ for now.