一千萬個為什麽

搜索

在vb.net中的linq表達式

任何正文都可以將以下c#代碼翻譯為vb。我試過telarik代碼轉換器,但我在expression.call遇到了問題,它根本不會編譯。

private static IOrderedQueryable OrderingHelper(IQueryable 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)source.Provider.CreateQuery(call);
}

謝謝 Thurein

最佳答案

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

轉載註明原文: 在vb.net中的linq表達式