1

我使用以下代码在应用程序中对列表进行排序,它工作正常。

public static class OrderByHelper
    {
        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "OrderBy");
        }
        public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "OrderByDescending");
        }
        public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "ThenBy");
        }
        public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "ThenByDescending");
        }
        static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
        {
            property = property.Trim();
            string[] props = property.Split('.');
            Type type = typeof(T);
            ParameterExpression arg = Expression.Parameter(type, "x");
            Expression expr = arg;
            foreach (string prop in props)
            {
                // use reflection (not ComponentModel) to mirror LINQ
                PropertyInfo pi = type.GetProperty(prop);
                expr = Expression.Property(expr, pi);
                type = pi.PropertyType;
            }
            Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
            LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

            object result = typeof(Queryable).GetMethods().Single(
                    method => method.Name == methodName
                            && method.IsGenericMethodDefinition
                            && method.GetGenericArguments().Length == 2
                            && method.GetParameters().Length == 2)
                    .MakeGenericMethod(typeof(T), type)
                    .Invoke(null, new object[] { source, lambda });
            return (IOrderedQueryable<T>)result;
        }

我称之为使用

 model.ProfileList = Helper.OrderByHelper.OrderBy(model.ProfileList.Cast<ProfileData>().AsQueryable(), ResultSet.SortField).ToList<ProfileData>();

现在我需要添加的是排序规则,因为我在内存中加载列表,所以在这里使用数据库排序规则没有意义。那么无论如何在代码中提供排序规则。可能吗?我用谷歌搜索,但什么也没找到。提前致谢。

例如:这里 å 是挪威字符,应该添加到列表的最后,但在默认排序规则中被视为 A

åtestå
cxvfg
Design
4

1 回答 1

0

这是我的解决方案,将来可能会对某人有所帮助。这是使用文化对内存列表进行排序的通用方法。

public class MyStringComparer : IComparer<string>
    {
        System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("no");
        public int Compare(string x, string y)
        {
            return string.Compare(x, y, true, culture);
        }
    }
    public static class OrderByHelper
    {
        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "OrderBy");
        }
        public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "OrderByDescending");
        }
        public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "ThenBy");
        }
        public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "ThenByDescending");
        }
        static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
        {
            property = property.Trim();
            string[] props = property.Split('.');
            Type type = typeof(T);
            ParameterExpression arg = Expression.Parameter(type, "x");
            Expression expr = arg;
            foreach (string prop in props)
            {
                // use reflection (not ComponentModel) to mirror LINQ
                PropertyInfo pi = type.GetProperty(prop);
                expr = Expression.Property(expr, pi);
                type = pi.PropertyType;
            }
            bool IsString = type.FullName == "System.String";

            Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
            MyStringComparer comparer = new MyStringComparer();
            LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
            object result = typeof(Queryable).GetMethods().Single(
                    method => method.Name == methodName
                            && method.IsGenericMethodDefinition
                            && method.GetGenericArguments().Length == 2
                            && (IsString ? method.GetParameters().Length == 3 : method.GetParameters().Length == 2))
                    .MakeGenericMethod(typeof(T), type)
                    .Invoke(null, IsString ? new object[] { source, lambda, comparer } : new object[] { source, lambda });
            return ((IOrderedQueryable<T>)result);
        }
    }
于 2014-05-08T13:42:21.270 回答