我有许多类型的集合,我想按不同的属性对每个集合进行排序。 例如, IEnumerable<Employee>将按Name和Age属性排序, IEnumerable<Department>将按NumberOfEmployees和DepartmentName属性排序。 我在排序后使用PaginatedList对集合进行分页。
public class PaginatedList<T> : List<T> { public PaginatedList(IEnumerable<T> source, Int32 pageIndex, Int32 pageSize , Func<T,Object> orderBy) { this.AddRange(source.OrderBy(orderBy).Skip((PageIndex - 1) * PageSize).Take(PageSize)); } }注意第4个参数,它是将传递给OrderBy扩展方法的排序委托。
我正在使用通用方法来生成第4个元素
public Func<T, Object> SortingFactory<T>(String sortby) { switch (typeof(T).ToString()) { case "Employee": switch(sortby) { case "Name": return new Func<Employee,String>(delegate(Employee e) { return e.Name; }); break; case "Age": return new Func<Employee,Int32>(delegate(Employee e) { return e.Age; }); break; } break; case "Department": switch(sortby) { case "NumberOfEmployees": return new Func<Department,Int32>(delegate(Department d) { return d.NumberOfEmployees; }); break; case "DepartmentName": return new Func<Department,String>(delegate(Department d) { return d.DepartmentName; }); break; } break; } }但它给了我一个编译错误Cannot implicitly convert type 'System.Func<Employee,String>' to 'System.Func<T,object>'
我也尝试将输出decalre为Func<Object,Object>但我得到了同样的错误。
我犯了什么错,怎么做这样的方法。
I have many collections for many types I want to sort each collection by different properties. For example IEnumerable<Employee> will be sorted by Name and Age properties,and IEnumerable<Department> will be sorted by NumberOfEmployees and DepartmentName properties. I use PaginatedList to paginating the collection after sorting it.
public class PaginatedList<T> : List<T> { public PaginatedList(IEnumerable<T> source, Int32 pageIndex, Int32 pageSize , Func<T,Object> orderBy) { this.AddRange(source.OrderBy(orderBy).Skip((PageIndex - 1) * PageSize).Take(PageSize)); } }Note the 4th parameter which is the sorting delegate that will be passed to OrderBy extension method.
I am using a generic method to generate this 4th element
public Func<T, Object> SortingFactory<T>(String sortby) { switch (typeof(T).ToString()) { case "Employee": switch(sortby) { case "Name": return new Func<Employee,String>(delegate(Employee e) { return e.Name; }); break; case "Age": return new Func<Employee,Int32>(delegate(Employee e) { return e.Age; }); break; } break; case "Department": switch(sortby) { case "NumberOfEmployees": return new Func<Department,Int32>(delegate(Department d) { return d.NumberOfEmployees; }); break; case "DepartmentName": return new Func<Department,String>(delegate(Department d) { return d.DepartmentName; }); break; } break; } }but it gives me a compilation ErrorCannot implicitly convert type 'System.Func<Employee,String>' to 'System.Func<T,object>'
I also tried to decalre the output as Func<Object,Object> but I got the same error.
What is the fault I made and how do such method.
最满意答案
说我理解得很好
public class PaginatedList<T> : List<T> { public PaginatedList(IEnumerable<T> source, Int32 pageIndex, Int32 pageSize ) { this.AddRange(GetOrderFor<T>().Skip((PageIndex - 1) * PageSize).Take(PageSize)); } } public static class Helpers { public static Func<T, object> GetSortExpression<T>(string sortExpressionStr) { var param = Expression.Parameter(typeof (T), "x"); var sortExpression = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(param, sortExpressionStr), typeof(object)), param); return sortExpression.Compile(); } public static IOrderedEnumerable<T> GetOrderFor<T>(this IEnumerable<T> list) { switch (typeof (T).Name) { case "Employee": return list.OrderBy(GetSortExpression<T>("Name")).ThenBy(GetSortExpression<T>("Age")); case "Category": return list.OrderBy(GetSortExpression<T>("Name")).ThenBy(GetSortExpression <T> ("Id")); } return null; } }如果我误解了,我认为GetSortExpression方法的简单用法可以帮助您避免错误
case "Employee": switch(sortby) { case "Name": return Helpers.GetSortExpression<T>("Name"); case "Age": return Helpers.GetSortExpression<T>("Age"); }Say I understood well
public class PaginatedList<T> : List<T> { public PaginatedList(IEnumerable<T> source, Int32 pageIndex, Int32 pageSize ) { this.AddRange(GetOrderFor<T>().Skip((PageIndex - 1) * PageSize).Take(PageSize)); } } public static class Helpers { public static Func<T, object> GetSortExpression<T>(string sortExpressionStr) { var param = Expression.Parameter(typeof (T), "x"); var sortExpression = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(param, sortExpressionStr), typeof(object)), param); return sortExpression.Compile(); } public static IOrderedEnumerable<T> GetOrderFor<T>(this IEnumerable<T> list) { switch (typeof (T).Name) { case "Employee": return list.OrderBy(GetSortExpression<T>("Name")).ThenBy(GetSortExpression<T>("Age")); case "Category": return list.OrderBy(GetSortExpression<T>("Name")).ThenBy(GetSortExpression <T> ("Id")); } return null; } }And if I misunderstood, I think the simple usage of GetSortExpression method can help you to avoid your error
case "Employee": switch(sortby) { case "Name": return Helpers.GetSortExpression<T>("Name"); case "Age": return Helpers.GetSortExpression<T>("Age"); }更多推荐
发布评论