2016-08-23 3 views
3

영어로 죄송합니다. .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; 
    } 
} 

답변

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]); 
    } 

이것은 SQL로 작동하지 않는 것이

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")); 
+0

당신이 실행할 수있는 방법이다. context.GetTable () .AsQueryable(). (o => o.SearchDeviceExpression().) 컴파일(). 호출 (searchStr))'예외를 throw합니다. '메서드 "System.Linq.Expressions.Expression'1 [System. Func'2 [System.String, System.Boolean]] SearchDeviceExpression() "SQL에 지원되는 변환이 없습니다." – Segment

관련 문제