I’m using a Func as an argument for a method to be used in a GroupBy operation in a Linq statement. TSource being IFoo and TKey being a property of IFoo such as IFoo.Number, making my Func parameter looking like:
foo => foo.Number
So far so good. However, now I’m trying to get a string representation of TKey or rather, I’m trying the coax the string “Number” from the Func<> parameter.
Can it be done and if so, how? Thanking you all in advance..
You can’t with a Func, you need a parameter of type
Expression<Func<TSource, TKey>>and work on Expression’s Body.
When you need the func, just compile the expression.
To get name from expression :
Retrieving Property name from lambda expression