Hi,
I'm trying to avoid a dynamic type in my lambda expression for grouping a collection. The type is defined anonymously at compile time (and unambiguously as far as I can tell). I'd rather not define the type as a full-fledged class as I'm only using it a few times in this single method.
Sample code:
Func<MyData, dynamic> dataGrouping = md => new
{
md.Property1,
md.Property2,
md.Property3
};
var groupedData = myDataCollection.GroupBy(dataGrouping);
While this will compile, it leaves me with no intellisense or strong typing inside the group as the type is dynamic.
I can't specify the type of dataGrouping as var, because I'm in C# and I get complaints of Cannot assign lambda expression to implicitly typed local variable.
Could I replace dynamic with the result of GetType() on the anonymous type? I'd then need the type before it's used in the lambda, but I can't see a useful way to get a handle on it before I'm already into the lambda itself.
Is there an elegant way of getting the type of this anonymous class?