DataTable runtime grouping on user defined criteria
Honestly, I am not sure if a dictionary is the right data structure for realizing composed keys. At least for me it is not directly obviously how the data are grouped. I think the main issue that confuses me is, that the grouping key contains not just the values to group by, but also the column headers which is absolutely irrelevant for grouping. Additionally I have to understand the comparer.



For your example, you could easily use a tuple which provides build-in comparison of each item:

var rowGroups = dataTable.AsEnumerable()
    .GroupBy(x =&gt; Tuple.Create(x["FirstName"], x["LastName"]))
    .ToList();


Another more flexible approach could be to create a string value to be used as grouping key:

var rowGroups = dataTable.AsEnumerable()
    .GroupBy(x =&gt; string.Join(":", keyColumns.Select(key =&gt; x[key])))
    .ToList();