C# – Linq Sorgularınızda OrderBy(“StringDeğer”) Kullanın

Aşağıdaki extension ile OrderBy() ve OrderByDescending() metodlarında String ifadeler kullanabilirsiniz.

Tanımlamalar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

Örnek Kullanım

                    IQueryable<LinqObjeTipiniz> objList = LinqObjeniz;
                    objList.OrderByDescending("SıralamaYapılacakObjeninAdı");
                    objList.OrderBy("SıralamaYapılacakObjeninAdı");

Extension

    public static class QueryableHelper
    {
        /// <summary>
        /// String ile order işlemi gerçekleştirir.
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="q"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static IQueryable<TModel> OrderBy<TModel>(this IQueryable<TModel> q, string name)
        {
            Type entityType = typeof(TModel);
            PropertyInfo p = entityType.GetProperty(name);
            MethodInfo m = typeof(QueryableHelper).GetMethod("OrderByProperty").MakeGenericMethod(entityType, p.PropertyType);
            return (IQueryable<TModel>)m.Invoke(null, new object[] { q, p });
        }

        /// <summary>
        /// String ile order işlemi gerçekleştirir.
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="q"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static IQueryable<TModel> OrderByDescending<TModel>(this IQueryable<TModel> q, string name)
        {
            Type entityType = typeof(TModel);
            PropertyInfo p = entityType.GetProperty(name);
            MethodInfo m = typeof(QueryableHelper).GetMethod("OrderByPropertyDescending").MakeGenericMethod(entityType, p.PropertyType);
            return (IQueryable<TModel>)m.Invoke(null, new object[] { q, p });
        }

        /// <summary>
        /// Propertyinfo ile order işlemi gerçekleştirir.
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TRet"></typeparam>
        /// <param name="q"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static IQueryable<TModel> OrderByPropertyDescending<TModel, TRet>(IQueryable<TModel> q, PropertyInfo p)
        {
            ParameterExpression pe = Expression.Parameter(typeof(TModel));
            Expression se = Expression.Convert(Expression.Property(pe, p), p.PropertyType);
            return q.OrderByDescending(Expression.Lambda<Func<TModel, TRet>>(se, pe));
        }

        /// <summary>
        /// Propertyinfo ile order işlemi gerçekleştirir.
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TRet"></typeparam>
        /// <param name="q"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static IQueryable<TModel> OrderByProperty<TModel, TRet>(IQueryable<TModel> q, PropertyInfo p)
        {
            ParameterExpression pe = Expression.Parameter(typeof(TModel));


            Expression se = Expression.Convert(Expression.Property(pe, p), p.PropertyType);
            return q.OrderBy(Expression.Lambda<Func<TModel, TRet>>(se, pe));
        }
    }