2010-05-11 70 views
1

任何機構可以將以下c#代碼轉換爲vb。我已經嘗試過telarik代碼轉換器,但是我在expression.call中遇到了問題,它根本無法編譯。在vb.net linq表達式

private static IOrderedQueryable<T> OrderingHelper<T>(IQueryable<T> source, string propertyName, bool descending, bool anotherLevel) 
{ 
    ParameterExpression param = Expression.Parameter(typeof(T), string.Empty); 
    MemberExpression property = Expression.PropertyOrField(param, propertyName); 
    LambdaExpression sort = Expression.Lambda(property, param); 

    MethodCallExpression call = Expression.Call( 
     typeof(Queryable), 
     (!anotherLevel ? "OrderBy" : "ThenBy") + (descending ? "Descending" : string.Empty), 
     new[] { typeof(T), property.Type }, // error line 
     source.Expression, 
     Expression.Quote(sort)); 

    return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(call); 
} 

感謝 Thurein

回答

0
Function foo(Of T)(ByVal source As IQueryable(Of T), ByVal propertyName As String, ByVal descending As Boolean, ByVal anotherLevel As Boolean) As Object 
    Dim param = Expression.Parameter(GetType(T), String.Empty) 
    Dim [property] = Expression.PropertyOrField(param, propertyName) 
    Dim sort = Expression.Lambda([property], param) 

    Dim [call] = Expression.Call(
    GetType(Queryable), 
    If(Not anotherLevel, "OrderBy", "ThenBy") & If(descending, "Descending", String.Empty), 
    New Type() {GetType(T), [property].Type}, 
    source.Expression, 
    Expression.Quote(sort)) 

    Return CType(source.Provider.CreateQuery(Of T)([call]), IOrderedQueryable(Of T)) 

End Function 
+0

您好,感謝烏拉圭回合的答覆,但我仍然有在這一行編譯錯誤 「新的(){的getType(T),property.Type}」 的說法該類型是預期的。 – Thurein 2010-05-11 03:35:04

+0

對不起,沒有編譯器。這是一個新版本。 – 2010-05-11 19:17:39

+0

VB 9:新類型(){GetType(T),[property] .Type}。 VB 10:{GetType(T),[property] .Type} – 2010-05-11 19:18:04