2

我有函数做动态 linq where to dbset on dbcontext 但得到错误 No method 'Where' exists on type System.Linq.IQueryable I don't now!!!

using System.Linq;

public virtual Queryable _List(string fieldNames="", string values="")
{
    _Db = Contex.Set<T>();
    var type = typeof(T);
    var property = type.GetProperty(fieldNames);
    var parameter = Expression.Parameter(type, "p");
    var propertyAccess = Expression.MakeMemberAccess(parameter, property);
    var orderByExp = Expression.Lambda(propertyAccess, parameter);

    var body2 = Expression.Call(
        typeof(Queryable),
        "Where",
        // I things this line no good but I don't now ...
        new Type[] { type, property.PropertyType },
        _Db.Expression,
        Expression.Quote(orderByExp));
    var m = _Db.Provider.CreateQuery<T>(body2);
    return m;
}
4

1 回答 1

2

没有重载Queryable.Where有两个类型参数。您可能正在使用这种方法:

IQueryable<TSource> Where<TSource> (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)

这意味着您应该替换该行:

new Type[] { type, property.PropertyType },

和:

new Type[] { type }, // this is TSource
于 2013-10-04T08:00:32.757 回答