【发布时间】:2010-10-21 00:03:58
【问题描述】:
我有这个扩展方法。
public static IQueryable<T> SearchBy<T>(this IQueryable<T> 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<T>(methodCallExpression);
}
我收到这个错误
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));
当我像这样从实体框架调用这个方法时
this.grdReservations.DataSource = dataContext.CustomerSet.SearchBy(crit);
这是搜索条件
SearchCriteria crit = new SearchCriteria();
crit.Property = "UserName";
crit.PropertyValue = "new_user";
crit.Search = SearchType.Equal;
如果有人能看一看并推动我朝着正确的方向前进,我会非常高兴。
感谢您的宝贵时间。
编辑:我在家,所以我无法测试,但我尝试的任何方法(“Select”、“Where”、“SelectMany”)都返回了这个错误,所以我假设我正在做其他事情错误的。
【问题讨论】:
标签: c# linq extension-methods