A.T.
A.T.

Reputation: 26332

Set Collation on IOrderedQueryable list

I use following code for sorting list in application and it is working fine.

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

I call it using

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

Now what i need to add is Collation, as i load list in-memory so using database collation has no sense here. So is there anyway to provide collation here in code. Is it possible? I google it but find nothing yet. Thanks in advance.

For example: here å is Norwegian character and should be added in last of the list, but it is treated as A in default collation

åtestå
cxvfg
Design

Upvotes: 1

Views: 623

Answers (1)

A.T.
A.T.

Reputation: 26332

Here is my solution, may be it help someone in future. This is generic way to sort in-memory list using culture.

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

Upvotes: 0

Related Questions