Tuesday, April 5, 2016

C# Sort two objects based on property names as string

I faced a situation where it was not possible to data in sql queries using any of the standard ways (Linq to SQL, Entity Framework, Stored Procedures, etc.) thus the data had to be sorted at controller method.

Little uncomfortable using static property names and wanted to pass the property name of the sort column as a string value, I ended up writing a custom comparer class as below

/// <summary>
    /// Compares two objects of same type based on value of the SortBy property
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SortComparer<T> : Comparer<T>
    {
        public string SortBy = string.Empty;
        public bool SortDescending = true;

        public SortComparer() { }

        public SortComparer(string sortBy, bool sortDescending)
        {
            SortBy = sortBy;
            SortDescending = sortDescending;
        }

        public override int Compare(T x, T y)
        {
            PropertyInfo propInfo = x.GetType().GetProperty(SortBy);
            if (propInfo != null)
            {
                var obj1 = propInfo.GetValue(x, new object[0]) as IComparable;
                var obj2 = propInfo.GetValue(y, new object[0]) as IComparable;
                if (obj1 != null)
                {
                    if (obj2 != null)
                    {
                        return SortDescending ? obj2.CompareTo(obj1) : obj1.CompareTo(obj2);
                    }
                    else
                    {
                        return SortDescending ? -1 : 1;
                    }
                }
                else if (obj2 != null)
                {
                    return SortDescending ? 1 : -1;
                }
            }
            return 0;
        }
    }

c# linq Sort by column name stored in a string variable

Frequently I have encountered the requirement to implent sort on individual columns in a table. I thought that the easiest and most intutive way of implementing this in MVC controller was to express the column name as a string variable value. But after a couple of attempts I realised that the default Sort method of IQueryable interface in linq does not work and hence we need to add our own method to do this task.

Google search quickly returned some nice results, but in short something like this should work.

private static IOrderedQueryable<T> OrderingHelper<T>(IQueryable<T> source, string propertyName, bool descending, bool anotherLevel)
{
    ParameterExpression param = Expression.Parameter(typeof(T), string.Empty); // I don't care about some naming
    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 },
        source.Expression,
        Expression.Quote(sort));
    return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(call);
}
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName)
{
    return OrderingHelper(source, propertyName, false, false);
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string propertyName)
{
    return OrderingHelper(source, propertyName, true, false);
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string propertyName)
{
    return OrderingHelper(source, propertyName, false, true);
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string propertyName)
{
    return OrderingHelper(source, propertyName, true, true);
}
 
 
 
More Links

Secure micro services using jwt and ocelot

  Secure Microservices Using JWT With Ocelot in .NET Core (code-maze.com)