通用分类器函数C#到VB

时间:2011-05-31 10:23:50

标签: .net vb.net generics sorting lambda

有人想尝试将其转换为VB吗?

public class GenericSorter<T, PT>
{
    public IEnumerable<T> Sort(IEnumerable source, string sortExpression)
    {
        var param = Expression.Parameter(typeof(T), "item");

        var sortLambda = Expression.Lambda<Func<T, PT>>(Expression.Convert(Expression.Property(param, sortExpression), typeof(PT)), param);

        return source.OfType<T>().AsQueryable<T>().OrderBy<T, PT>(sortLambda);
    }

    public IEnumerable<T> Sort(IEnumerable<T> source, string sortExpression)
    {
        var param = Expression.Parameter(typeof(T), "item");

        var sortLambda = Expression.Lambda<Func<T, PT>>(Expression.Convert(Expression.Property(param, sortExpression), typeof(PT)), param);

        return source.AsQueryable<T>().OrderBy<T, PT>(sortLambda);
    }
}

据我所知......

Public Class GenericSorter(Of T, PT)
    Public Function Sort(ByVal source As IEnumerable, ByVal sortExpression As String) As IEnumerable(Of T)
        Dim param = Expression.Parameter(GetType(T), "item")

        Dim sortLambda = Expression.Lambda(Of Func(Of T, PT))(Expression.Convert(Expression.[Property](param, sortExpression), GetType(PT)), param)
        Dim s As IEnumerable(Of T) = source.OfType(Of T).AsQueryable().OrderBy(Of T, PT) (Of sortLambda)

        Return s
    End Function

应该对任何类型的对象进行排序。取自here

1 个答案:

答案 0 :(得分:2)

DeveloperFusions的转换器(here)返回了这个:

Public Class GenericSorter(Of T, PT)
    Public Function Sort(source As IEnumerable, sortExpression As String) As IEnumerable(Of T)
        Dim param = Expression.Parameter(GetType(T), "item")

        Dim sortLambda = Expression.Lambda(Of Func(Of T, PT))(Expression.Convert(Expression.[Property](param, sortExpression), GetType(PT)), param)

        Return source.OfType(Of T)().AsQueryable(Of T)().OrderBy(Of T, PT)(sortLambda)
    End Function

    Public Function Sort(source As IEnumerable(Of T), sortExpression As String) As IEnumerable(Of T)
        Dim param = Expression.Parameter(GetType(T), "item")

        Dim sortLambda = Expression.Lambda(Of Func(Of T, PT))(Expression.Convert(Expression.[Property](param, sortExpression), GetType(PT)), param)

        Return source.AsQueryable(Of T)().OrderBy(Of T, PT)(sortLambda)
    End Function
End Class

它可能值得一试。