2010-05-11 4 views
1

Может ли любое тело перевести следующий код C# в vb. Я пробовал конвертер кода telarik, но у меня проблема с expression.call, и он вообще не будет компилироваться.выражение linq в vb.net

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

+0

Извините, сделал это без компилятора. Вот новая версия. –

+0

VB 9: Новый тип() {GetType (T), [property] .Type}. VB 10: {GetType (T), [property] .Type} –