Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am have a problem with Dynamically generated compared to a regular lambda expression in code. Below is the code that I use to generate a dynamic lambda.

Dynamic Lambda
C#
var parameterExp = Expression.Parameter(typeof(T), "x");
var propertyExp = Expression.Property(parameterExp, propertyName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });

var someValue = Expression.Constant(propertyValue, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);

return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);


Lambda from the debug view
C#
{x => x.Name.Contains("UserName")}


But when I do the lambda in code like this
C#
x => x.Name.Contains(formattedText);

I get this for a result. FormattedText is the variable used for regular lambda it = "UserName".
C#
{x => x.Name.Contains(value(Test.BusinessLogicLayer.ClassBLL+<>x__DisplayClass8).formattedText)}


I use the lambda on the where clause on an Iqueryable object. The dynamic lambda generates the SQL in Iqueryable with no SQL parameters. The regular lambda causes the SQL to have parameters. I would need it to generate with parameters. We are looping through the parameters to change the like to a contains so we can use Full Text search in SQL server.

My questions is, is there a way to dynamically generate the lambda to have the exact same output as the regular lambda? Or is there a better way to use Full Text Search in Entity Framework 6?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900