0

I have one Linq expression and is executing properly by below code

Expression expressionMain=(Param_0.Email ?? "").StartsWith("Test@abc.com", OrdinalIgnoreCase)
var lambda = Expression.Lambda<Func<CustomModel, bool>>(expressionMain, paramExpr);
var compiledLambda = lambda.Compile();
var queryResult = TableEntity.AsQueryable()
                 .Where(compiledLambda)
                 .ToList();

but if i get that same expression as a string

"(Param_0.Email ?? "").StartsWith("Test@abc.com", OrdinalIgnoreCase)" 

from some other source(sql), then how to convert/cast this to Expression and execute by above code?

UPDATES after checking the comments,I tried both links

Solution 1

Solution 2

Finally the Linq expression formart in both answer is differnt than mine.The expression will look like

{((Param_0.LocationId == Convert(46, Int32)) And (Param_0.DepartmentId == Convert(1, Nullable`1)))}

I am looking for a solution with this type of Expression

user3501613
  • 596
  • 7
  • 28
  • Does this answer your question? [How to convert a String to its equivalent LINQ Expression Tree?](https://stackoverflow.com/questions/821365/how-to-convert-a-string-to-its-equivalent-linq-expression-tree) – MindSwipe Nov 22 '21 at 12:45
  • Does this answer your question? [Parsing embedded expressions in Roslyn](https://stackoverflow.com/questions/28566732/parsing-embedded-expressions-in-roslyn) – phuzi Nov 22 '21 at 12:52
  • Param_0 is the parameter identifier and Email is a column in my table where i executing the above expression – user3501613 Nov 22 '21 at 13:03
  • I am getting proper result if i am passing "(Param_0.Email ?? "").StartsWith("Test@abc.com", OrdinalIgnoreCase)" this as expression but if i save this as string am not able to convert/cast back to expression – user3501613 Nov 22 '21 at 13:06
  • It is not proper way to work with expressions. Why you have converted them to string? – Svyatoslav Danyliv Nov 22 '21 at 14:25
  • I need to use this expression in other places also,so saving same in the DB, Whenever i need result i will take and execute – user3501613 Nov 22 '21 at 14:29
  • @MindSwipe , @phuzi ,, I tried both answers, but in both cases the expression which is used is in different format,, my expression will look like this "{((Param_0.LocationId == Convert(46, Int32)) And (Param_0.DepartmentId == Convert(1, Nullable`1)))}" – user3501613 Nov 22 '21 at 14:33

0 Answers0