using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Data; using System.Linq; using System.Linq.Expressions; namespace BatchService.Framework.Utility { public static class QueryableExtension { public static IOrderedQueryable OrderBy(this IQueryable query, string propertyName) { return _OrderBy(query, propertyName, false); } public static IOrderedQueryable OrderByDescending(this IQueryable query, string propertyName) { return _OrderBy(query, propertyName, true); } static IOrderedQueryable _OrderBy(IQueryable query, string propertyName, bool isDesc) { string methodname = (isDesc) ? "OrderByDescendingInternal" : "OrderByInternal"; var memberProp = typeof(T).GetProperty(propertyName); var method = typeof(QueryableExtension).GetMethod(methodname) .MakeGenericMethod(typeof(T), memberProp.PropertyType); return (IOrderedQueryable)method.Invoke(null, new object[] { query, memberProp }); } public static IOrderedQueryable OrderByInternal(IQueryable query, PropertyInfo memberProperty) {//public return query.OrderBy(_GetLamba(memberProperty)); } public static IOrderedQueryable OrderByDescendingInternal(IQueryable query, PropertyInfo memberProperty) {//public return query.OrderByDescending(_GetLamba(memberProperty)); } static Expression> _GetLamba(PropertyInfo memberProperty) { if (memberProperty.PropertyType != typeof(TProp)) throw new Exception(); var thisArg = Expression.Parameter(typeof(T)); var lamba = Expression.Lambda>(Expression.Property(thisArg, memberProperty), thisArg); return lamba; } } }