Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I nest a call to a method within a call to a method using Expression.Call
    primarykey
    data
    text
    <p>I am trying to convert a DateTime to a String before calling contains on it. However despite my efforts of putting the result of one expression into another I fail miserably. </p> <p>The code is derived from the highest answer to this <a href="https://stackoverflow.com/questions/2413032/jqgrid-with-asp-net-webmethod-and-json-working-with-sorting-paging-searching-an" title="jqgrid with asp.net webmethod and json working with sorting, paging, searching and LINQ — but needs dynamic operators">question</a> jqgrid with asp.net webmethod and json working with sorting, paging, searching and LINQ — but needs dynamic operators.</p> <p>Assume I have the following method as StringExtension from the <a href="https://stackoverflow.com/questions/2413032/jqgrid-with-asp-net-webmethod-and-json-working-with-sorting-paging-searching-an" title="jqgrid with asp.net webmethod and json working with sorting, paging, searching and LINQ — but needs dynamic operators">question</a>:</p> <pre><code>public static class StringExtensions { public static MemberExpression ToMemberExpression(this string source, ParameterExpression p) { if (p == null) throw new ArgumentNullException("p"); string[] properties = source.Split('.'); Expression expression = p; Type type = p.Type; foreach (var prop in properties) { var property = type.GetProperty(prop); if (property == null) throw new ArgumentException("Invalid expression", "source"); expression = Expression.MakeMemberAccess(expression, property); type = property.PropertyType; } return (MemberExpression)expression; } } </code></pre> <p>Therefore I have the following method also from the <a href="https://stackoverflow.com/questions/2413032/jqgrid-with-asp-net-webmethod-and-json-working-with-sorting-paging-searching-an" title="jqgrid with asp.net webmethod and json working with sorting, paging, searching and LINQ — but needs dynamic operators">question</a> which I have then adapted for DateTime.</p> <pre><code>public virtual Expression&lt;Func&lt;T, bool&gt;&gt; CreateExpression&lt;T&gt;(string searchField, string searchString, string searchOper) { Expression exp = null; var p = Expression.Parameter(typeof(T), "p"); Expression propertyAccess = searchField.ToMemberExpression(p); switch (searchOper) { case "bw": exp = Expression.Call(propertyAccess, typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }), Expression.Constant(searchString)); break; // New code by me case "cn": if (propertyAccess.Type == typeof(DateTime)) { // My faulty logic - from Jon Skeet answer below Expression toStringCall = Expression.Call( propertyAccess, "ToString", null, new[] { Expression.Constant("D") }); Expression containsCall = Expression.Call( toStringCall, "Contains", null, new[] { Expression.Constant(searchString) }); exp = containsCall; } else { // Unchanged exp = Expression.Call(propertyAccess, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(searchString)); } break; case "ew": exp = Expression.Call(propertyAccess, typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) }), Expression.Constant(searchString)); break; case "gt": exp = Expression.GreaterThan(propertyAccess, Expression.Constant(searchString, propertyAccess.Type)); break; case "ge": exp = Expression.GreaterThanOrEqual(propertyAccess, Expression.Constant(searchString, propertyAccess.Type)); break; case "lt": exp = Expression.LessThan(propertyAccess, Expression.Constant(searchString, propertyAccess.Type)); break; case "le": exp = Expression.LessThanOrEqual(propertyAccess, Expression.Constant(searchString, propertyAccess.Type)); break; case "eq": exp = Expression.Equal(propertyAccess, Expression.Constant(searchString.ToType(propertyAccess.Type), propertyAccess.Type)); break; case "ne": exp = Expression.NotEqual(propertyAccess, Expression.Constant(searchString, propertyAccess.Type)); break; default: return null; } return (Expression&lt;Func&lt;T, bool&gt;&gt;)Expression.Lambda(exp, p); } </code></pre> <p>I get the following exception.</p> <p>LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload