is there a good way to do a Linq GroupBy
where the grouping key is determined at runtime? e.g. I want the grouping key to be built from a user-selected list of
All you need is to construct a Func
var arg = Expression.Parameter(typeof(Item), "item");
var body = Expression.Property(arg, "D");
var lambda = Expression.Lambda>(body, arg);
var keySelector = lambda.Compile();
Usage:
var result = source.GroupBy(keySelector);
It gets slightly (but not much) more difficult if you don't know the type of the property at compile-time.