GENERATING A LAMBDA BY PASSING IN A PROPERTY NAME FOR USE IN .COUNT() WITH WHERE CONDITION

Goti Bandu

Total Post:119

Points:835
Posted by  Goti Bandu
C# 
C#
 861  View(s)
Ratings:
Rate this:

I'm trying to generate a lambda expression, by passing in the property name to filter on as a string, then I  need to count the results where an "Enum" is set to a certain status

var expressionParam = Expression.Parameter(typeof (ArtworkPage), "page");

var body = Expression.Property(expressionParam, property);

var lambda = Expression.Lambda(body, expressionParam,).Compile();

int approvedList = GetArtworkUploadPages(artwork.Id).Count(lambda);

How do I get the final piece of the puzzle in passing what enum value I want to filter by into the lambda?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Generating a lambda by passing in a property name for use in .Count() with Where condition

    Hi Goti,


    You need to compare the property with a constant value:

    var lambda = Expression.Lambda<Func<ArtworkPage, bool>>(

        Expression.Equal(

            Expression.Property(expressionParam, property),

            Expression.Constant(yourEnumValue)

        ),

        experssionParam

    );    

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!