抱歉我的英文。我在.NET 3.5中有LINQ to SQL查询,并希望在SearchDeviceExpression方法中重用DeviceExpression方法。像这样的东西
public static Expression<Func<DEVICE_TYPE, bool>> SearchDeviceExpression2(string s)
{
return o => DEVICE_TYPE.DeviceExpression().Compile()(o) == s;
}
在运行时,我得到异常Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL
。所以没有该方法的翻译,但可能存在任何解决方法吗?
我的课。
[Table(Name = "DEVICE_TYPE_LOCAL")]
public class DEVICE_TYPE
{
[Column]
public string DEV_CODE
{
get;
set;
}
[Column]
public string DEVICE_NAME
{
get;
set;
}
[Column]
public string DEVICE_MARK
{
get;
set;
}
public static Expression<Func<DEVICE_TYPE, string>> DeviceExpression()
{
return o => "(" + o.DEV_CODE + ") " + o.DEVICE_NAME + " " + o.DEVICE_MARK;
}
public static Expression<Func<DEVICE_TYPE, bool>> SearchDeviceExpression(string s)
{
return o => "(" + o.DEV_CODE + ") " + o.DEVICE_NAME + " " + o.DEVICE_MARK == s;
}
}
答案 0 :(得分:1)
您必须从表达式DeviceExpression返回创建一个新表达式,并返回一个返回字符串的表达式,然后您必须执行该表达式并将参数传递给它。像这样:
public Expression<Func<string>> DeviceExpression()
{
return () => "(" + DEV_CODE + ") " + DEVICE_NAME + " " + DEVICE_MARK;
}
public Expression<Func<string,bool>> SearchDeviceExpression()
{
Expression<Func<string,string>> exp = (string s) => s;
return Expression.Lambda<Func<string, bool>>(Expression.Equal(DeviceExpression().Body, exp.Body), exp.Parameters[0]);
}
以下是如何执行它
var t = new DEVICE_TYPE();
t.DEV_CODE = "a";
t.DEVICE_NAME = "b";
t.DEVICE_MARK = "c";
Console.Write(t.SearchDeviceExpression().Compile().Invoke("(a) b c"));