none
Lambda reflection RRS feed

  • Question

  • Good morning,


    Needed to create a generic lambda expression.

    Example: Clientes.Bill.Where(B => B.TOTAL > 1000)

    How can I create a complete expression for reflection? including the Lambda parameter?

    Best Regards

    • Moved by Mike DanesModerator Thursday, December 27, 2012 1:28 PM not WPF (From:Windows Presentation Foundation (WPF))
    Thursday, December 27, 2012 1:16 PM

Answers

  • Something like this maybe:

    Expression<Func<T, bool>> GenerateExpression<T>()
    {
        ParameterExpression B = Expression.Parameter(typeof(T));
        return Expression.Lambda<Func<T, bool>>(
            Expression.GreaterThan(
                Expression.PropertyOrField(B, "TOTAL"),
                Expression.Constant(1000)),
            B);
    }

    • Proposed as answer by JMCF125 Monday, December 31, 2012 12:58 PM
    • Marked as answer by Lisa ZhuModerator Friday, January 4, 2013 5:36 AM
    Monday, December 31, 2012 9:38 AM

All replies

  • Good morning,


    Needed to create a generic lambda expression.

    Example: Clientes.Bill.Where(B => B.TOTAL > 1000)

    How can I create a complete expression for reflection? including the Lambda parameter?

    Best Regards

    See the following C# Lambda Expressions Guide: http://msdn.microsoft.com/en-us/library/bb397687.aspx
    Thursday, December 27, 2012 2:03 PM
  • Something like this maybe:

    Expression<Func<T, bool>> GenerateExpression<T>()
    {
        ParameterExpression B = Expression.Parameter(typeof(T));
        return Expression.Lambda<Func<T, bool>>(
            Expression.GreaterThan(
                Expression.PropertyOrField(B, "TOTAL"),
                Expression.Constant(1000)),
            B);
    }

    • Proposed as answer by JMCF125 Monday, December 31, 2012 12:58 PM
    • Marked as answer by Lisa ZhuModerator Friday, January 4, 2013 5:36 AM
    Monday, December 31, 2012 9:38 AM