zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    js 实现商品放大镜效果
    Angular.js数据绑定时自动转义html标签及内容
    Sublime 快捷键一览表
    这么牛的简历,你见过么?
    正向代理与反向代理
    负载均衡
    redis搭建与安装
    svn服务配置和日常维护命令
    Mysql导出导入
    linux搭建svn服务器
  • 原文地址:https://www.cnblogs.com/leeolevis/p/1776509.html
Copyright © 2011-2022 走看看