R.K
R.K

Reputation: 13

Get Value of Property inside a Lambda Expression using Reflection

I have an object called SearchDetails which contains:

SearchDetails: 
{ ColName: "StrName"
  SearchVal" "mega" }

I am making a generic lambda expression by using reflection method.

    public dynamic searchMethod(object SearchDetails)
    {
        ParameterExpression Parameter = Expression.Parameter(typeof(SearchDetails), "x");

        var searchCol = Expression.Property(
         Parameter,
         SearchDetails.GetType().GetProperty("ColName")

       );
        var colVal = Expression.Property(
          Parameter,
          SearchDetails.GetType().GetProperty("SearchValue").Name
        );

       Expression contMethod = Expression.Call(searchCol, "Contains", null, colVal);
        Expression<Func<SearchDetails, bool>> lambda =
           Expression.Lambda<Func<SearchDetails, bool>>(contMethod, Parameter);

        return lambda;
    }

The problem is that I am getting lambda expressions as follow:

{x => x.ColName.Contains(x.SearchValue)}

However, I want it to be like this: {x => x.StrName.Contains("megabrand")}. I cannot access the value of the properties: ColName and SearchValue. How to solve this problem?

Upvotes: 0

Views: 1730

Answers (1)

xanatos
xanatos

Reputation: 111930

What you are looking for is probably something similar to this:

public static Expression<Func<TSource, bool>> SearchMethod<TSource>(SearchDetails searchDetails)
{
    ParameterExpression par = Expression.Parameter(typeof(TSource), "x");

    var col = Expression.Property(par, searchDetails.ColName);

    Expression body = Expression.Call(col, "Contains", null, Expression.Constant(searchDetails.SearchVal));

    var lambda = Expression.Lambda<Func<TSource, bool>>(body, par);
    return lambda;
}

Note that you have to pass the type of your table somewhere, in this case as a generic parameter TSource.

Use it like:

var search = new SearchDetails
{
    ColName = "Foo",
    SearchVal = "Bar",
};

var lambda = SearchMethod<TbStore>(search);

As an alternative you could use System.Linq.Dynamic.Core to obtain something similar.

var result = db.Where(searchDetails.ColName + ".Contains(@0)", searchDetails.SearchVal);

Upvotes: 1

Related Questions