0

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..

Paul Siersma
  • 2,036
  • 1
  • 22
  • 25

1 Answers1

4

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

Community
  • 1
  • 1
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122