的问题是,LINQ到实体不支持编译表情,而你编译表达和试图调用它们。但是,LINQ试图将它们解析为表达式并将它们转换为SQL代码,并且它不能这样做。 而你想通过text
和value
作为不同的参数,所以你不能合并它们。
你可以这样做:
public static IQueryable<DropDownListItem> ToDropDownList<TSource>(IQueryable<TSource> query, Expression<Func<TSource, DropDownListItem>> value)
{
return query.Select(value);
}
ToDropDownList<RubricEntity>(_service.RubricAsQueryable(), x => new DropDownListItem() { Value = x.Id, Text = x.DisplayName });
但对于我来说,这has't有很大的意义...
如果你仍然想通过他们作为独立的参数,那么你就可以尝试在运行时将它们结合起来是这样的:
public static IQueryable<DropDownListItem> ToDropDownList<TSource>(IQueryable<TSource> query, Expression<Func<TSource, long>> value, Expression<Func<TSource, string>> text)
{
Expression<Func<TSource, DropDownListItem>> func = x => new DropDownListItem
{
Value = 1,
Text = "1"
};
var replacer = new ExpressionReplacer<TSource>()
{
Text = text,
Value = value,
Parameter = func.Parameters[0] // we will take X parameter
};
var convertedFunc = replacer.Visit(func) as Expression<Func<TSource, DropDownListItem>>;
return query.Select(convertedFunc);
}
private class ExpressionReplacer<TSource> : ExpressionVisitor
{
public Expression<Func<TSource, long>> Value { get; set; }
public Expression<Func<TSource, string>> Text { get; set; }
public ParameterExpression Parameter { get; set; }
protected override Expression VisitConstant(ConstantExpression node)
{
if (node.Type == typeof(long))
return this.Visit(Value.Body);
if (node.Type == typeof(string))
return this.Visit(Text.Body);
return base.VisitConstant(node);
}
protected override Expression VisitParameter(ParameterExpression node)
{
// we will replace all usage to X. it has the same type, but it isn't linked to expiression
return Parameter;
}
}
ToDropDownList<RubricEntity>(_service.RubricAsQueryable(), x => x.Key, x => x.Value);
基本上我们刚创建的常量值存根表达,然后根据不同类型随附在paremeters表达式替换常数值。然后发送替换表达式选择的方法,所以它finaly表达式将类似于:
Select(x => new DropDownListItem() { Value = x.Id, Text = x.DisplayName })
您好我得到了这样的错误“X”未在指定的LINQ到实体绑定的参数查询表达式。感谢您的帮助。 – 2014-11-25 10:58:26
关于如何解决这个问题的任何想法? – 2014-11-25 19:30:37
嗨。嗯你是对的。我更新了'ExpressionReplacer'的答案,并轻微更改了'ToDropDownList'方法。我用EF本地检查它,它工作正常。 – 2014-11-25 22:36:13