我在 asp.net mvc 3.0 应用程序中使用实体框架。我正在使用表值函数对我的一个数据库表进行全文搜索。我已将返回类型设置为 Request 类型的对象。然后,我将结果输入到表达式构建器中,该构建器对来自用户有权访问的实体列表中的请求执行包含操作。这是代码。
List<int> UserEntities = db.UserEntities
.Where(x => x.UserID.Equals(usr.ID))
.Select(x => x.EntityID).ToList();
var requests = db.SearchRequest(keyword)
.Where(BuildContainsExpression<Request, int>(x => x.EntityID, UserEntities));
//Expression Builder for Contains SQL functionality
static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(
Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
if (null == valueSelector) { throw new ArgumentNullException("valueSelector"); }
if (null == values) { throw new ArgumentNullException("values"); }
ParameterExpression p = valueSelector.Parameters.Single();
// p => valueSelector(p) == values[0] || valueSelector(p) == ...
if (!values.Any())
{
return e => false;
}
var equals = values.Select(
value => (Expression)Expression.Equal(
valueSelector.Body,
Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate<Expression>(
(accumulate, equal) => Expression.Or(accumulate, equal));
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
第二行将不会构建。编译器告诉我 SearchRequest 过程正在返回 ObjectResult 但需要 IQueryable。如何将 ObjectResult 转换为 IQueryable?