Linq之动态排序(字符传入)

复制代码
static class Program  
{                                                                                                                                                                                       
    
static void Main(string[] args)                                                          
                                                                                             
    {                                                                                        
                                                                                             
        
string[] array = { "aaa""bb""c" };                                               
                                                                                             
        
foreach (var item in array.AsQueryable().OrderBy("Length"))                          
                                                                                             
        {                                                                                    
                                                                                             
            Console.WriteLine(item);                                                         
                                                                                             
        }                                                                                    
                                                                                             
        Console.ReadLine();                                                                  
                                                                                             
    }                                                                                        
                                                                                             
                                                                                             
                                                                                             
    
public static IQueryable<T> OrderBy<T>(this IQueryable<T> query, string prop)            
                                                                                             
    {                                                                                        
                                                                                             
        var member 
= typeof(T).GetProperty(prop);                                            
                                                                                             
        
return (IQueryable<T>)typeof(Program)                                                
                                                                                             
            .GetMethod(
"OrderByCore", BindingFlags.Static | BindingFlags.NonPublic)          
                                                                                             
            .MakeGenericMethod(
typeof(T), member.PropertyType)                               
                                                                                             
            .Invoke(
nullnew object[] { query, member });                                   
                                                                                             
    }                                                                                        
                                                                                             
                                                                                             
                                                                                             
    
private static IQueryable<T> OrderByCore<T, TMember>(IQueryable<T> query, MemberInfo memb
                                                                                             
    {                                                                                        
                                                                                             
        ParameterExpression item;                                                            
                                                                                             
        
return query.OrderBy<T, TMember>(Expression.Lambda<Func<T, TMember>>(                
                                                                                             
            Expression.MakeMemberAccess(item 
= Expression.Parameter(typeof(T), "item"), membe
                                                                                             
    }                                                                                        
                                                                                             
}                                                                                            
复制代码

 

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

posted @   leeolevis  阅读(348)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示