Sorry for my english. I have LINQ to SQL query in .NET 3.5 and want to reuse DeviceExpression method in SearchDeviceExpression method. Something like that
public static Expression<Func<DEVICE_TYPE, bool>> SearchDeviceExpression2(string s)
{
return o => DEVICE_TYPE.DeviceExpression().Compile()(o) == s;
}
In runtime I get exception Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL. So it's true that no translation for that method, but maybe exists any workaround?
My class.
[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;
}
}
You must create a new expression from the expression DeviceExpression returns and an expression that returns a string, then you must execute that expression and pass the parameters to it. Like so:
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]);
}
Here is how you can execute it
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With