tags:

views:

125

answers:

1

Hi,

Can any body translate the following c# code to vb. I have tried telarik code converter but I got problem at expression.call and it won't compile at all.

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

thanks Thurein

A: 
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
Jonathan Allen
Hi, thanks for ur reply, but I still got compile error on this line " new () { getType(T), property.Type }, " saying that the type is expected.
Thurein
Sorry, did that without a compiler. Here is a new version.
Jonathan Allen
VB 9: New Type() {GetType(T), [property].Type}. VB 10: {GetType(T), [property].Type}
Jonathan Allen
Hi i got that, actually I changed the new() to new type() which i m not sure its correct or not, but it sure solved my compile error and work fine , thanks.
Thurein