LINQ中的动态排序

使用Linq动态属性排序

使用反射: 

ExpandedBlockStart.gif 代码
  public   static  Func < T,Tkey >  DynamicLambda < T, Tkey > ( string  propertyName)
        {
         
            ParameterExpression p 
=  Expression.Parameter( typeof (T),  " p " );
            Expression body 
=  Expression.Property(p,  typeof (T).GetProperty(propertyName));
            
            var lambda 
=  Expression.Lambda < Func < T, Tkey >> (body, p);
           
            
return  lambda.Compile();
        }

 

调用:

ExpandedBlockStart.gif 代码
 List < Employee >  list  =   new  List < Employee > ();
            list.Add(
new  Employee() { Name  =   " 张三 " , Age  =   21 , Salary  =  1800f, Job  =   " UI "  });
            list.Add(
new  Employee() { Name  =   " 李四 " , Age  =   25 , Salary  =  2000f, Job  =   " DBA "  });
            list.Add(
new  Employee() { Name  =   " 王五 " , Age  =   24 , Salary  =  2000f, Job  =   " UI "  });
            list.Add(
new  Employee() { Name  =   " 李九 " , Age  =   31 , Salary  =  2900f, Job  =   " DBA "  });
            list.Add(
new  Employee() { Name  =   " 张一 " , Age  =   21 , Salary  =  2100f, Job  =   " UI "  });
            list.Add(
new  Employee() { Name  =   " 王三 " , Age  =   32 , Salary  =  2100f, Job  =   " DBA "  });
  
             
// 按Age排序
            list.OrderBy(DynamicLambda < Employee, int > ( " Age " ));
            
// 按Salary排序
            
//  list.OrderBy(DynamicLambda<Employee,float>("Salary"));

            list.ForEach(e 
=>  Console.WriteLine(e.Name  +   " \t "   +  e.Age  +   " \t "   +  e.Salary));

 

 

转载于:https://www.cnblogs.com/Qbit/archive/2010/06/26/1765694.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用.NETLINQ进行动态排序可以通过使用Dynamic LINQ库来实现。Dynamic LINQ库是由Microsoft提供的一个扩展库,允许我们在运行时构建和执行动态LINQ查询。 首先,我们需要在项目添加Dynamic LINQ库的引用,这可以通过从NuGet包管理器安装Dynamic LINQ包来完成。 在代码,我们可以使用OrderBy和ThenBy方法来进行动态排序。这两个方法可以接收一个字符串参数作为排序表达式。这个表达式可以是字段名称、属性名称或任何适用的表达式。 例如,假设我们有一个存储学生信息的列表,并且我们希望根据学生的年龄进行排序: ```csharp using System.Linq; using System.Linq.Dynamic; class Program { static void Main(string[] args) { List<Student> students = new List<Student>() { new Student() { Name = "Tom", Age = 20 }, new Student() { Name = "Jerry", Age = 18 }, new Student() { Name = "Alice", Age = 22 } }; Console.WriteLine("按年龄升序排序:"); var sortedStudents = students.OrderBy("Age"); foreach (var student in sortedStudents) { Console.WriteLine(student.Name); } Console.WriteLine("按年龄降序排序:"); sortedStudents = students.OrderByDescending("Age"); foreach (var student in sortedStudents) { Console.WriteLine(student.Name); } Console.ReadLine(); } } class Student { public string Name { get; set; } public int Age { get; set; } } ``` 上述代码,我们通过字符串参数"Age"来对学生列表进行排序。OrderBy方法将按照升序对学生列表进行排序,而OrderByDescending方法将按照降序进行排序。 通过以上代码,我们可以根据需要动态指定排序字段,并根据要求进行升序或降序排序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值