動態linq
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(); }
應用:
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));
相關文章
- LINQ 動態排序排序
- 強大的動態 LINQ 庫
- [C#.NET 拾遺補漏]13:動態構建LINQ查詢表示式C#
- Linq
- Lambda、Linq
- linq介紹
- 瞭解LINQ
- linQ基礎
- [go-linq]-Go的.NET LINQ式查詢方法Go
- LinQ查詢基礎(三)LINQ to ADO.net(1)LINQ to DataSet實現複雜資料查詢
- Linq使用Group By 1
- Linq之左聯接
- Linq 日期轉換
- LINQ擴充套件方法套件
- linq學習筆記筆記
- C# 之 Linq to XmlC#XML
- Linq之分組聯接
- Linq sum()時遇到NULLNull
- Linq 綜合寫法
- 【Linq】常用語法彙總
- LINQ SelectMany的應用場景
- ASP.Net LINQ to SQl查詢ASP.NETSQL
- DLL動態庫動態載入
- java動態代理動態在哪裡?Java
- linq介紹及工作中應用兩例——左聯與內聯,linq迴圈方法
- System.Linq.Dynamic字串轉委託字串
- linq中AsEnumerable和AsQueryable的區別
- 【轉】LINQ to SQL語句(1)之WhereSQL
- C#規範整理·集合和LinqC#
- C# LINQ (語言整合查詢)C#
- Linq查詢之多個排序條件排序
- Linq兩個from查詢條件
- 靜態動態陣列陣列
- Java代理(jdk靜態代理、動態代理和cglib動態代理)JavaJDKCGLib
- 動態 DP
- 動態代理
- 動態圖
- 動態 REMREM
- 動態REMREM