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) Output List (after Height sorting )");
70.
71. for (int I = 0, len = list. Count; I <len; I ++)
72 .{
73. Console. WriteLine ("{0} {1}", list [I]. Height, tempList [I]. Height );
74 .&

Related Article

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.