[英]cannot convert from 'System.Linq.Expressions.Expression<System.Func<T, V?>>' to 'System.Linq.Expressions.Expression<System.Func<T, decimal>>'
[英]Cannot convert expression type 'lambda expression' to return type 'System.Linq.Expressions.Expression<System.Func<IProduct,string,bool>>'
好的,我迷路了。 为什么第一个函数为WRONG(在lambda表达式中为squiglies),但第二个函数是RIGHT(意味着它编译)?
public static Expression<Func<IProduct, string, bool>> IsValidExpression(string val)
{
return (h => h.product_name == val);
}
public static Expression<Func<IProduct, bool>> IsValidExpression2()
{
return (m => m.product_name == "ACE");
}
你的第一个函数需要两个参数。 Func<x,y,z>
定义了两个参数和返回值。 既然你有一个IProduct
和一个string
作为参数,你的lambda中需要两个参数。
public static Expression<Func<IProduct, string, bool>> IsValidExpression(string val)
{
return ((h, i) => h.product_name == val);
}
你的第二个函数只是Func<x,y>
,这意味着函数签名只有一个参数,因此你的lambda语句编译。
什么是中间string
打算做什么? 你可以通过以下方式编译:
public static Expression<Func<IProduct, string, bool>> IsValidExpression(string val)
{
return (h,something) => h.product_name == val;
}
或者你的意思是:
public static Expression<Func<IProduct, string, bool>> IsValidExpression()
{
return (h,val) => h.product_name == val;
}
?
Func<IProduct, string, bool>
是具有以下签名的方法的委托:
bool methodName(IProduct product, string arg2)
{
//method body
return true || false;
}
所以
public static Expression<Func<IProduct, string, bool>> IsValidExpression(string val)
{
return (h => h.product_name == val);
}
返回类型和返回值之间存在差异。 您正在尝试返回Expression<Func<IProduct, bool>>
类型的对象。
val参数不是你所委托的方法的参数,而是将被提升(作为实现结果函数的类的一部分),并且因为它不是结果方法的参数,所以它不应该是Func
类型声明的一部分
在尝试修复lambda表达式之前,请确保将以下引用添加到相关的cs文件中:
using System.Linq;
using System.Linq.Expressions;
缺少这些引用也可能导致相同的错误( “无法将lambda表达式转换为类型'System.Linq.Expressions.Lambda Expression',因为它不是委托类型” )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.