Note that there are some explanatory texts on larger screens.

plurals
  1. POCompare with string values using IQueryable<T> and Expressions
    primarykey
    data
    text
    <p>Sorry about the title but I can't really think of a quick way of saying what I want - could someone please change it to a more appropriate one if you think of one?</p> <p>I'm trying to turn the following function call into an expression query.</p> <pre><code>List.Compare("propretyName", "&gt;1000"); public static IQueryable&lt;T&gt; Compare&lt;T&gt;(this IQueryable&lt;T&gt; source, string propertyName, string value) { Type type = typeof(T); ParameterExpression parameter = Expression.Parameter(type, "param"); MemberExpression memberAccess = Expression.MakeMemberAccess(parameter, type.GetProperty(propertyName)); string comparisonType = value.Substring(0, 1); value = value.Length &gt; 0 ? value.Substring(1) : "0"; decimal tmpvalue; decimal? result = decimal.TryParse(value, out tmpvalue) ? tmpvalue : (decimal?)null; ConstantExpression constant = Expression.Constant(result, typeof(decimal?)); BinaryExpression comparisonExpression = Expression.GreaterThan(memberAccess, constant); switch (comparisonType) { case "&gt;": comparisonExpression = Expression.GreaterThan(memberAccess, constant); break; case "&lt;": comparisonExpression = Expression.LessThan(memberAccess, constant); break; case "=": comparisonExpression = Expression.Equal(memberAccess, constant); break; } Expression&lt;Func&lt;T, bool&gt;&gt; lambda = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(comparisonExpression, parameter); return source.Where(lambda); } </code></pre> <p>The above is the method I wrote to make that call.</p> <p>The lambda at the bottom appears to be correct: <code>{param =&gt; (param.propretyName &gt; 1000)}</code></p> <p>However, it's not working and I think it's because the particular proprety I'm working on is a <code>decimal?</code> so it should be <code>{param =&gt; (param.propertyName.Value &gt; 1000)}</code>.</p> <p>Could anybody help me out to use the Value rather than. There's just something that's escaping me here.</p> <p>I can't use the <code>Where(string)</code> method as I'm using Entity-Framework.</p> <h1>Answer found</h1> <pre><code>public static IQueryable&lt;T&gt; Compare&lt;T&gt;(this IQueryable&lt;T&gt; source, string propertyName, string value) { Type type = typeof(T); ParameterExpression parameter = Expression.Parameter(type, "param"); MemberExpression memberAccess = Expression.MakeMemberAccess(parameter, type.GetProperty(propertyName)); //This is the added methods that results in the proper output PropertyInfo valProp = typeof(Nullable&lt;decimal&gt;).GetProperty("Value"); memberAccess = Expression.MakeMemberAccess(memberAccess, valProp); string comparisonType = value.Substring(0, 1); value = value.Length &gt; 0 ? value.Substring(1) : "0"; decimal tmpvalue; decimal? result = decimal.TryParse(value, out tmpvalue) ? tmpvalue : (decimal?)null; ConstantExpression constant = Expression.Constant(tmpvalue); BinaryExpression comparisonExpression = Expression.GreaterThan(memberAccess, constant); switch (comparisonType) { case "&gt;": comparisonExpression = Expression.GreaterThan(memberAccess, constant); break; case "&lt;": comparisonExpression = Expression.LessThan(memberAccess, constant); break; case "=": comparisonExpression = Expression.Equal(memberAccess, constant); break; } Expression&lt;Func&lt;T, bool&gt;&gt; lambda = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(comparisonExpression, parameter); return source.Where(lambda); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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