1

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?

7
  • It is very unclear what you are asking... We don't know what StrName is or where you could get the string "megabrand" cames from... Commented Jul 4, 2018 at 9:29
  • Then it isn't clear why you can't simply have a SearchDetails searchDetails as the parameter of the searchMethod Commented Jul 4, 2018 at 9:30
  • @xanatos I am having an object at first of my question called SearchDetails which includes the key ColName and its Value: "StrName". (SearchDetails: { ColName: "StrName" SearchVal" "mega" })I am using this method because I have to make generic lambda expression where in order to be able to send it many general parameters Commented Jul 4, 2018 at 9:35
  • Are you trying to create lamdas of type Expression<Func<dynamic, bool>>? Because I can't comprehend what you are trying to do. If SearchDetails is the "descriptor" of what search you want to do, what is the object where the search is done? What is the type of x in {x => x.StrName.Contains("megabrand")}? Commented Jul 4, 2018 at 9:40
  • @xanatos I am working with database. In my database I have a table where I should get the search result from it. The result I want to get is: Expression<Func<TbStore, bool>> lambda = Expression.Lambda<Func<TbStore, bool>>(contMethod, Parameter); where TbStore is the database table that I am working with. The searchDetail is the object that contains the input text that the user inputs and the column name that the user want his search results Commented Jul 4, 2018 at 9:54

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.