I have tried to convert the string to ToLower case using the below Expression call.
var tolowerMethod = typeof(string).GetMethods().Where(m => m.Name == "ToString").FirstOrDefault();
var toLowerMethodCall = Expression.Call(memExp,tolowerMethod,new Expression[0]);
I am facing some issue to create an Expression call to format a value like: "05/12/2012 12:00:00" to {0:MM/dd/yyyy}.
Well there's no such method that can take a date string in one format and reformat it to another. You'd have to convert that string to a DateTime then back to a string.
Here's how you could create such a lambda:
var dateStr = Expression.Parameter(typeof(string));
// call static method "DateTime.Parse"
var asDateTime = Expression.Call(typeof(DateTime), "Parse", null, dateStr);
var fmtExpr = Expression.Constant("MM/dd/yyyy");
// call instance method "DateTime.ToString(string)"
var body = Expression.Call(asDateTime, "ToString", null, fmtExpr);
var lambdaExpr = Expression.Lambda<Func<string, string>>(body, dateStr);
which is equivalent to:
(string date) => DateTime.Parse(date).ToString("MM/dd/yyyy");
Then compile and call it.
var method = lambdaExpr.Compile();
method("05/12/2012 12:00:00"); // "05/12/2012"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With