动态构建OrderBy的Lambda表达式
view plaincopy to clipboardprint?
01.public class Employee
02. {
03. public int ID { get; set; }
04. public string Name { get; set; }
05. public decimal Pay { get; set; }
06. public float Height { get; set; }
07. }
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Pay { get; set; }
public float Height { get; set; }
}
view plaincopy to clipboardprint?
01.class Program
02. {
03. static void Main(string[] args)
04. {
05.
06. IList<Employee> list = new List<Employee>();
07.
08. list.Add(new Employee() { ID = 2, Name = "D", Height = 175.50f, Pay = 6000.45m });
09. list.Add(new Employee() { ID = 4, Name = "A", Height = 178.00f, Pay = 4500.8m });
10. list.Add(new Employee() { ID = 5, Name = "C", Height = 173.54f, Pay = 7500.11m });
11. list.Add(new Employee() { ID = 1, Name = "B", Height = 180.15f, Pay = 8000.2m });
12. list.Add(new Employee() { ID = 3, Name = "E", Height = 182.3f, Pay = 5500.70m });
13.
14. Console.WriteLine("输出List (ID排序前)");
15.
16. StringBuilder builder = new StringBuilder();
17.
18. foreach (var i in list)
19. {
20. builder.AppendFormat("{0},", i.ID);
21. }
22.
23. Console.WriteLine(builder.ToString().TrimEnd(','));
24. Console.WriteLine("");
25.
26. Console.WriteLine("输出List (ID排序后)");
27.
28. list = IListOrderBy<Employee>(list, "ID");
29.
30. builder.Length = 0;
31.
32. foreach (var i in list)
33. {
34. builder.AppendFormat("{0},", i.ID);
35. }
36.
37. Console.WriteLine(builder.ToString().TrimEnd(','));
38. Console.WriteLine("");
39.
40.
41. Console.WriteLine("输出List (Name排序前)");
42.
43. builder.Length = 0;
44.
45. foreach (var i in list)
46. {
47. builder.AppendFormat("{0},", i.Name);
48. }
49.
50. Console.WriteLine(builder.ToString().TrimEnd(','));
51. Console.WriteLine("");
52.
53. Console.WriteLine("输出List (Name排序后)");
54.
55. list = IListOrderBy<Employee>(list, "Name");
56.
57. builder.Length = 0;
58.
59. foreach (var i in list)
60. {
61. builder.AppendFormat("{0},", i.Name);
62. }
63.
64. Console.WriteLine(builder.ToString().TrimEnd(','));
65. Console.WriteLine("");
66.
67. var tempList = IListOrderBy<Employee>(list, "Height");
68.
69. Console.WriteLine("输出List (Height排序前)\t\t输出List (Height排序后)");
70.
71. for (int i = 0, len = list.Count; i < len; i++)
72. {
73. Console.WriteLine("\t{0}\t\t\t\t\t{1}", list[i].Height, tempList[i].Height);
74. }
75.
76. Console.WriteLine("");
77.
78. tempList = IListOrderBy<Employee>(list, "Pay");
79.
80. Console.WriteLine("输出List (Pay排序前)\t\t输出List (Pay排序后)");
81.
82. for (int i = 0, len = list.Count; i < len; i++)
83. {
84. Console.WriteLine("\t{0}\t\t\t\t{1}", list[i].Pay, tempList[i].Pay);
85. }
86.}
class Program
{
static void Main(string[] args)
{
IList<Employee> list = new List<Employee>();
list.Add(new Employee() { ID = 2, Name = "D", Height = 175.50f, Pay = 6000.45m });
list.Add(new Employee() { ID = 4, Name = "A", Height = 178.00f, Pay = 4500.8m });
list.Add(new Employee() { ID = 5, Name = "C", Height = 173.54f, Pay = 7500.11m });
list.Add(new Employee() { ID = 1, Name = "B", Height = 180.15f, Pay = 8000.2m });
list.Add(new Employee() { ID = 3, Name = "E", Height = 182.3f, Pay = 5500.70m });
Console.WriteLine("输出List (ID排序前)");
StringBuilder builder = new StringBuilder();
foreach (var i in list)
{
builder.AppendFormat("{0},", i.ID);
}
Console.WriteLine(builder.ToString().TrimEnd(','));
Console.WriteLine("");
Console.WriteLine("输出List (ID排序后)");
list = IListOrderBy<Employee>(list, "ID");
builder.Length = 0;
foreach (var i in list)
{
builder.AppendFormat("{0},", i.ID);
}
Console.WriteLine(builder.ToString().TrimEnd(','));
Console.WriteLine("");
Console.WriteLine("输出List (Name排序前)");
builder.Length = 0;
foreach (var i in list)
{
builder.AppendFormat("{0},", i.Name);
}
Console.WriteLine(builder.ToString().TrimEnd(','));
Console.WriteLine("");
Console.WriteLine("输出List (Name排序后)");
list = IListOrderBy<Employee>(list, "Name");
builder.Length = 0;
foreach (var i in list)
{
builder.AppendFormat("{0},", i.Name);
}
Console.WriteLine(builder.ToString().TrimEnd(','));
Console.WriteLine("");
var tempList = IListOrderBy<Employee>(list, "Height");
Console.WriteLine("输出List (Height排序前)\t\t输出List (Height排序后)");
for (int i = 0, len = list.Count; i < len; i++)
{
Console.WriteLine("\t{0}\t\t\t\t\t{1}", list[i].Height, tempList[i].Height);
}
Console.WriteLine("");
tempList = IListOrderBy<Employee>(list, "Pay");
Console.WriteLine("输出List (Pay排序前)\t\t输出List (Pay排序后)");
for (int i = 0, len = list.Count; i < len; i++)
{
Console.WriteLine("\t{0}\t\t\t\t{1}", list[i].Pay, tempList[i].Pay);
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Sandy945/archive/2010/07/14/5735326.aspx