0

I have some method:

public void Foo(Expression<Func<TModel, IEnumerable>> expression) {
  // foo
}

and I have variable:

Expression<Func<TModel, IList<TItem>>> expression;

How I can pass my variable to Foo?

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
  • With **much** effort. See my own question [here](http://stackoverflow.com/q/2797261/50079); read at the very least [my answer](http://stackoverflow.com/a/10467736/50079) (as the accepted one does not answer the most important part of your question) *and* the comments. I 'm not marking it as a dupe, leaving that for you to decide. – Jon May 25 '12 at 10:25

2 Answers2

4

You have to convert your expression variable:

var exp = Expression.Lambda<Func<TModel, IEnumerable>>(expression.Body, expression.Parameters);
Foo(exp);
Balazs Tihanyi
  • 6,659
  • 5
  • 23
  • 24
2

Use this:

    public Expression<Func<TModel, IEnumerable>> ConvertExpression<TModel, TItem>(Expression<Func<TModel, IList<TItem>>> expression)
    {
        return (Expression<Func<TModel, IEnumerable>>)Expression
            .Lambda(Expression.Convert(expression.Body, typeof(IEnumerable)), expression.Parameters);
    }
Dennis
  • 37,026
  • 10
  • 82
  • 150