vb.net 中的 linq 表达式

发布于 2024-09-01 06:05:04 字数 880 浏览 3 评论 0原文

任何机构都可以将以下 C# 代码翻译为 vb 吗?我尝试过 telarik 代码转换器,但在 expression.call 时遇到问题,它根本无法编译。

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

谢谢 苏雷因

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2024-09-08 06:05:04
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文