I am working on the application where user can select columns he/she wants to see on the screen and which columns to group by or aggregate. So, in my LINQ section I should a
There are several ways to do this. Here's one.
Below is a class I use pretty often called NTuple. It is the same idea as the Tuple
Given a set of columns
// as per OP, the list of columns to group by will be generated at runtime
IEnumerable columnsToGroupBy = ...;
you can use the NTuple class to group by those columns like this:
var groups = dt.AsEnumerable()
.GroupBy(r => new NTuple
Here's the beef:
public class NTuple : IEquatable>
{
public NTuple(IEnumerable values)
{
Values = values.ToArray();
}
public readonly T[] Values;
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
return true;
if (obj == null)
return false;
return Equals(obj as NTuple);
}
public bool Equals(NTuple other)
{
if (ReferenceEquals(this, other))
return true;
if (other == null)
return false;
var length = Values.Length;
if (length != other.Values.Length)
return false;
for (var i = 0; i < length; ++i)
if (!Equals(Values[i], other.Values[i]))
return false;
return true;
}
public override int GetHashCode()
{
var hc = 17;
foreach (var value in Values)
hc = hc*37 + (!ReferenceEquals(value, null) ? value.GetHashCode() : 0);
return hc;
}
}
Here's a test case:
static void Main(string[] args)
{
// some sample data
var dt = new DataTable();
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("CITY", typeof(string));
dt.Columns.Add("STATE", typeof(string));
dt.Columns.Add("VALUE", typeof(double));
dt.Rows.Add("Mike", "Tallahassee", "FL", 3);
dt.Rows.Add("Mike", "Tallahassee", "FL", 6);
dt.Rows.Add("Steve", "Tallahassee", "FL", 5);
dt.Rows.Add("Steve", "Tallahassee", "FL", 10);
dt.Rows.Add("Steve", "Orlando", "FL", 7);
dt.Rows.Add("Steve", "Orlando", "FL", 14);
dt.Rows.Add("Mike", "Orlando", "NY", 11);
dt.Rows.Add("Mike", "Orlando", "NY", 22);
// some "configuration" data
IEnumerable columnsToGroupBy = new[] {"CITY", "STATE"};
string columnToAggregate = "VALUE";
// the test routine
foreach (var group in dt.AsEnumerable().GroupBy(r => new NTuple