Note that there are some explanatory texts on larger screens.

plurals
  1. POExtension Method with dynamically built linq predicate
    text
    copied!<p>I have this extension method.</p> <pre><code> public static IQueryable&lt;T&gt; SearchBy&lt;T&gt;(this IQueryable&lt;T&gt; source, SearchCriteria criteria) { //throw an error if the source is null if (source == null) { throw new ArgumentNullException("source"); } ParameterExpression parameter = Expression.Parameter(source.ElementType, string.Empty); var property = Expression.Equal(Expression.PropertyOrField(parameter, criteria.Property), Expression.Constant(criteria.PropertyValue)); LambdaExpression lambda = Expression.Lambda(property, parameter); Expression methodCallExpression = Expression.Call(typeof(Queryable), "SelectMany", new Type[] { source.ElementType, property.Type }, source.Expression, Expression.Quote(lambda)); return source.Provider.CreateQuery&lt;T&gt;(methodCallExpression); } </code></pre> <p>I get this error </p> <pre><code> No method 'SelectMany' on type 'System.Linq.Queryable' is compatible with the supplied arguments. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: No method 'SelectMany' on type 'System.Linq.Queryable' is compatible with the supplied arguments. Source Error: Line 67: LambdaExpression lambda = Expression.Lambda(property, parameter); Line 68: Line 69: Expression methodCallExpression = Expression.Call(typeof(Queryable), "SelectMany", Line 70: new Type[] { source.ElementType, property.Type }, Line 71: source.Expression, Expression.Quote(lambda)); </code></pre> <p>when I call this method from the entity framework like this</p> <pre><code>this.grdReservations.DataSource = dataContext.CustomerSet.SearchBy(crit); </code></pre> <p>this is SearchCriteria </p> <pre><code> SearchCriteria crit = new SearchCriteria(); crit.Property = "UserName"; crit.PropertyValue = "new_user"; crit.Search = SearchType.Equal; </code></pre> <p>If anyone could take a look and give me a push in the right direction I'd be very happy. </p> <p>Thanks for your time. </p> <p>Edit: I'm home so I can't test but any method I try ("Select", "Where", "SelectMany") have all returned this error, so I'm assuming I'm doing something else wrong. </p>
 

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