Reflection and Linq Extension Methods
-
I have this code:
public static IQueryable<T> OrderBy<T>(
this IQueryable<T> source, string propertyName, string order)
{
var type = typeof(T);
var asc = order == "asc" ? true:false;
string methodName = asc ? "OrderBy" : "OrderByDescending";var parameter = Expression.Parameter(type, "p"); var property = type.GetProperty(propertyName); var propertyAccess = Expression.MakeMemberAccess(parameter, property); var orderByExp = Expression.Lambda(propertyAccess, parameter); MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName, new Type\[\] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExp)); return source.Provider.CreateQuery<T>(resultExp); }
And i would like to extend it to allow child properties, like "Address.Street" if i pass just a simple property it works nice... but i want to make it better and accept child object property to make order by. Anyone knows how to modify it? I tryed but no success... i don't know how to make it to find the child object property and create the MemberAccess with it X| Thanks!!
-
I have this code:
public static IQueryable<T> OrderBy<T>(
this IQueryable<T> source, string propertyName, string order)
{
var type = typeof(T);
var asc = order == "asc" ? true:false;
string methodName = asc ? "OrderBy" : "OrderByDescending";var parameter = Expression.Parameter(type, "p"); var property = type.GetProperty(propertyName); var propertyAccess = Expression.MakeMemberAccess(parameter, property); var orderByExp = Expression.Lambda(propertyAccess, parameter); MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName, new Type\[\] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExp)); return source.Provider.CreateQuery<T>(resultExp); }
And i would like to extend it to allow child properties, like "Address.Street" if i pass just a simple property it works nice... but i want to make it better and accept child object property to make order by. Anyone knows how to modify it? I tryed but no success... i don't know how to make it to find the child object property and create the MemberAccess with it X| Thanks!!
There you go, sorts on properties of properties... not quite sure if it works with methods. The logic is there, but it's not fully tested
public static IEnumerable SortObject(this IEnumerable ObjectToSort, string SortBy) { if (ObjectToSort == null) return null; if (string.IsNullOrEmpty(SortBy) == true) return null; // Handle sorting on object methods as well as properties string\[\] tempSorts = SortBy.Split(','); List SortList = new List(); bool inParanthese = false; string addString = string.Empty; foreach (string tempSort in tempSorts) { if ((tempSort.Contains("(") == true) && (tempSort.Contains(")") == false)) { inParanthese = true; } else if (tempSort.Contains(")") == true) { inParanthese = false; } if (inParanthese == false) { if (string.IsNullOrEmpty(addString) == false) { SortList.Add(addString + "," + tempSort.Trim()); } else { SortList.Add(tempSort); } addString = string.Empty; } else { if (string.IsNullOrEmpty(addString) == false) addString += ","; addString += tempSort.Trim(); } } string\[\] Sorts = SortList.ToArray(); bool Sorted = false; foreach (string FieldSort in Sorts) { string\[\] SubSort = FieldSort.Trim().Split(' '); string\[\] props = SubSort\[0\].Split('.'); Type type = typeof(T); ParameterExpression arg = Expression.Parameter(type, "x"); Expression expr = arg; foreach (string prop in props) { System.Reflection.PropertyInfo pi = type.GetProperty(prop); if (pi != null) { Sorted = true; //expr = Expression.Property(expr, pi); expr = Expression.MakeMemberAccess(expr, pi);