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

  • 相关阅读:
    八数码
    狂神说笔记——多线程05
    狂神说笔记——Java SE基础03
    从零开始的卷积神经网络
    深度学习与机器学习的区别
    模型的评估与选择
    经验风险VS风险函数
    常见的损失函数
    Erlang聊天室
    uniapp APP端 清除缓存
  • 原文地址:https://www.cnblogs.com/leeolevis/p/1776509.html
Copyright © 2011-2022 走看看