Dynamically construct the lambda expression of orderby

Source: Internet
Author: User
Tags sorted by name

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.45 m });
09. List. Add (new employee () {id = 4, name = "A", Height = 178.00f, pay = 4500.8 m });
10. List. Add (new employee () {id = 5, name = "C", Height = 173.54f, pay = 7500.11 m });
11. List. Add (new employee () {id = 1, name = "B", Height = 180.15f, pay = 8000.2 m });
12. List. Add (new employee () {id = 3, name = "E", Height = 182.3f, pay = 5500.70 m });
13.
14. Console. writeline ("output list (before ID sorting )");
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 ("output list (sorted by 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 ("output list (before name sorting )");
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 ("output list (sorted by 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 ("output list (before height sorting) \ t output list (after height sorting )");
70.
71. For (INT I = 0, Len = List. Count; I <Len; I ++)
72 .{
73. console. writeline ("\ t {0} \ t {1}", list [I]. height, templist [I]. height );
74 .}
75.
76. Console. writeline ("");
77.
78. templist = ilistorderby <employee> (list, "Pay ");
79.
80. Console. writeline ("output list (before pay sorting) \ t output list (after pay sorting )");
81.
82. For (INT I = 0, Len = List. Count; I <Len; I ++)
83 .{
84. Console. writeline ("\ t {0} \ 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.45 m });
List. Add (new employee () {id = 4, name = "A", Height = 178.00f, pay = 4500.8 m });
List. Add (new employee () {id = 5, name = "C", Height = 173.54f, pay = 7500.11 m });
List. Add (new employee () {id = 1, name = "B", Height = 180.15f, pay = 8000.2 m });
List. Add (new employee () {id = 3, name = "E", Height = 182.3f, pay = 5500.70 m });

Console. writeline ("output list (before ID sorting )");

Stringbuilder builder = new stringbuilder ();

Foreach (var I in List)
{
Builder. appendformat ("{0},", I. ID );
}

Console. writeline (builder. tostring (). trimend (','));
Console. writeline ("");

Console. writeline ("output list (sorted by 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 ("output list (before name sorting )");

Builder. Length = 0;

Foreach (var I in List)
{
Builder. appendformat ("{0},", I. Name );
}

Console. writeline (builder. tostring (). trimend (','));
Console. writeline ("");

Console. writeline ("output list (sorted by 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 ("output list (before height sorting) \ t output list (after height sorting )");

For (INT I = 0, Len = List. Count; I <Len; I ++)
{
Console. writeline ("\ t {0} \ t {1}", list [I]. Height, templist [I]. Height );
}

Console. writeline ("");

Templist = ilistorderby <employee> (list, "Pay ");

Console. writeline ("output list (before pay sorting) \ t output list (after pay sorting )");

For (INT I = 0, Len = List. Count; I <Len; I ++)
{
Console. writeline ("\ t {0} \ t {1}", list [I]. Pay, templist [I]. Pay );
}
}

 

 

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/Sandy945/archive/2010/07/14/5735326.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.