乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      LINQ操作符四:排序操作符

       張小龍net館藏 2018-01-31

      排序操作符,包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序。

      OrderBy操作符將序列中的元素按照升序排列。

      注意:orderby必須在select之前出現(xiàn),查詢表達(dá)式最后只可能出現(xiàn)select或者groupby。

      student類:

      復(fù)制代碼
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Linq;
       4 using System.Text;
       5 using System.Threading.Tasks;
       6 
       7 namespace 排序操作符
       8 {
       9     public class Student
      10     {
      11 
      12         //姓名
      13         public string Name { get; set; }
      14         //成績(jī)
      15         public int Score { get; set; }
      16         public int Order { get; set; }
      17         //構(gòu)造函數(shù)
      18         public Student(string name, int score, int order)
      19         {
      20             this.Name = name;
      21             this.Score = score;
      22             this.Order = order;
      23         }
      24     }
      25 }
      復(fù)制代碼

      teacher類:

      復(fù)制代碼
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Linq;
       4 using System.Text;
       5 using System.Threading.Tasks;
       6 
       7 namespace 排序操作符
       8 {
       9     /// <summary>
      10     /// Teacher類
      11     /// </summary>
      12     public class Teacher
      13     {
      14         //姓名
      15         public string Name { get; set; }
      16         //學(xué)生集合
      17         public List<Student> Students { get; set; }
      18 
      19         public Teacher(string name, List<Student> students)
      20         {
      21             this.Name = name;
      22             this.Students = students;
      23         }
      24     }
      25 }
      復(fù)制代碼

      Program:

      復(fù)制代碼
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Linq;
       4 using System.Text;
       5 using System.Threading.Tasks;
       6 
       7 namespace 排序操作符
       8 {
       9     class Program
      10     {
      11         static void Main(string[] args)
      12         {
      13             //使用集合初始化器初始化Teacher集合
      14             List<Teacher> teachers = new List<Teacher> { 
      15                new Teacher("徐老師",
      16                new List<Student>(){
      17                  new Student("宋江",80,1),
      18                 new Student("盧俊義",95,2),
      19                 new Student("朱武",45,3)
      20                }
      21                ),
      22                 new Teacher("姜老師",
      23                new List<Student>(){
      24                  new Student("林沖",90,2),
      25                 new Student("花榮",85,4),
      26                 new Student("柴進(jìn)",58,5)
      27                }
      28                ),
      29                 new Teacher("樊老師",
      30                new List<Student>(){
      31                  new Student("關(guān)勝",100,6),
      32                 new Student("阮小七",70,7),
      33                 new Student("時(shí)遷",30,0)
      34                }
      35                )
      36             };
      37 
      38             #region OrderBy
      39             //OrderBy 表達(dá)式
      40             var queryOrderBy = from t in teachers
      41                                from s in t.Students
      42                                where s.Score < 60
      43                                orderby s.Order
      44                                select s;
      45             //方法
      46             var query = teachers.SelectMany(p => p.Students).Where(s => s.Score < 60).OrderBy(s => s.Order).Select(s => s);
      47             foreach (var item in queryOrderBy)
      48             {
      49                 Console.WriteLine("姓名:"+item.Name+",分?jǐn)?shù):"+item.Score);
      50             }
      51             foreach (var item in query)
      52             {
      53                 Console.WriteLine("姓名:" + item.Name + ",分?jǐn)?shù):" + item.Score);
      54             } 
      55             #endregion
      56 
      57             Console.ReadKey();
      58         }
      59     }
      60 }
      復(fù)制代碼

       

      運(yùn)行結(jié)果:

      二、OrderByDescending

      OrderByDescending操作符將序列中的元素按照降序排列。
      示例:
      復(fù)制代碼
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Linq;
       4 using System.Text;
       5 using System.Threading.Tasks;
       6 
       7 namespace 排序操作符
       8 {
       9     class Program
      10     {
      11         static void Main(string[] args)
      12         {
      13             //使用集合初始化器初始化Teacher集合
      14             List<Teacher> teachers = new List<Teacher> { 
      15                new Teacher("徐老師",
      16                new List<Student>(){
      17                  new Student("宋江",80,1),
      18                 new Student("盧俊義",95,2),
      19                 new Student("朱武",45,3)
      20                }
      21                ),
      22                 new Teacher("姜老師",
      23                new List<Student>(){
      24                  new Student("林沖",90,2),
      25                 new Student("花榮",85,4),
      26                 new Student("柴進(jìn)",58,5)
      27                }
      28                ),
      29                 new Teacher("樊老師",
      30                new List<Student>(){
      31                  new Student("關(guān)勝",100,6),
      32                 new Student("阮小七",70,7),
      33                 new Student("時(shí)遷",30,0)
      34                }
      35                )
      36             };
      37 
      38             #region OrderByDescending
      39             //表達(dá)式
      40             var queryDesc = from t in teachers
      41                             from s in t.Students
      42                             where s.Score < 60
      43                             orderby s.Order descending
      44                             select s;
      45             //方法
      46             var query1 = teachers.SelectMany(p => p.Students).Where(s => s.Score < 60).OrderByDescending(s => s.Order).Select(s => s);
      47             foreach (var item in queryDesc)
      48             {
      49                 Console.WriteLine("姓名:" + item.Name + ",分?jǐn)?shù):" + item.Score);
      50             }
      51             foreach (var item in queryDesc)
      52             {
      53                 Console.WriteLine("姓名:" + item.Name + ",分?jǐn)?shù):" + item.Score);
      54             }
      55             #endregion
      56 
      57             Console.ReadKey();
      58         }
      59     }
      60 }
      復(fù)制代碼

       運(yùn)行效果:

      三、ThenBy排序

      ThenBy操作符實(shí)現(xiàn)按照次關(guān)鍵字對(duì)序列進(jìn)行升序排列。此操作符的查詢語(yǔ)法和方法語(yǔ)法略有不同,例如:

      //查詢語(yǔ)法
      var query=from e in Employees orderby e.FirstName,e.LastName  select e;
      //方法語(yǔ)法
      var q=Employees.OrderBy(e=>e.FirstName).ThenBy(e=>e.LastName).Select(e=>e);

      示例:

      Category類:

      復(fù)制代碼
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Linq;
       4 using System.Text;
       5 using System.Threading.Tasks;
       6 
       7 namespace ThenBy
       8 {
       9     public class Category
      10     {
      11         public Guid CategoryId { get; set; }
      12         public string CategoryName { get; set; }
      13         public int Order { get; set; }
      14     }
      15 }
      復(fù)制代碼

      Products類:

      復(fù)制代碼
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Linq;
       4 using System.Text;
       5 using System.Threading.Tasks;
       6 
       7 namespace ThenBy
       8 {
       9     public class Products
      10     {
      11         public Guid Id { get; set; }
      12         public Guid CategoryId { get; set; }
      13         public string Name { get; set; }
      14         public int Order { get; set; }
      15         public DateTime CreateTime { get; set; }
      16     }
      17 }
      復(fù)制代碼

      Program:

      復(fù)制代碼
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Linq;
       4 using System.Text;
       5 using System.Threading.Tasks;
       6 
       7 namespace ThenBy
       8 {
       9     class Program
      10     {
      11         static void Main(string[] args)
      12         {
      13             List<Products> pro = new List<Products>() { 
      14                new Products(){Id=Guid.NewGuid(),Name="格林童話",CategoryId=Guid.NewGuid(),Order=1,CreateTime=DateTime.Now.AddHours(2)},
      15                new Products(){Id=Guid.NewGuid(),Name="美女與野獸",CategoryId=Guid.NewGuid(),Order=1,CreateTime=DateTime.Now},
      16                new Products(){Id=Guid.NewGuid(),Name="世界如此險(xiǎn)惡,您的內(nèi)心需要強(qiáng)大",CategoryId=Guid.NewGuid(),Order=0,CreateTime=DateTime.Now.AddHours(3)}            
      17             };
      18             //查詢語(yǔ)法
      19             var query = (from p in pro orderby p.Order, p.CreateTime select p);
      20             //方法
      21             var q = pro.OrderBy(p => p.Order).ThenBy(p => p.CreateTime).Select(p => p);
      22             foreach (var item in query)
      23             {
      24                 Console.WriteLine("name:"+item.Name+"id:"+item.Id+",order:"+item.Order+",CreateTime:"+item.CreateTime);
      25             }
      26             foreach (var item in q)
      27             {
      28                 Console.WriteLine("name:" + item.Name + "id:" + item.Id + ",order:" + item.Order + ",CreateTime:" + item.CreateTime);
      29             }
      30 
      31             Console.ReadKey();
      32         }
      33     }
      34 }
      復(fù)制代碼

      運(yùn)行效果:

      四、ThenByDescending

      ThenByDescending操作符實(shí)現(xiàn)按照次關(guān)鍵字對(duì)序列進(jìn)行降序排列。此操作符的查詢語(yǔ)法與方法語(yǔ)法略有不同,例如:
      //查詢語(yǔ)法
      var query=from e in Employees orderby e.FirstName,e.LastName descending  select e;
      //方法語(yǔ)法
      var q=Employees.OrderBy(e=>e.FirstName).ThenByDescending(e=>e.LastName).Select(e=>e);
      示例:
      復(fù)制代碼
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Linq;
       4 using System.Text;
       5 using System.Threading.Tasks;
       6 
       7 namespace ThenBy
       8 {
       9     class Program
      10     {
      11         static void Main(string[] args)
      12         {
      13             List<Products> pro = new List<Products>() { 
      14                new Products(){Id=Guid.NewGuid(),Name="格林童話",CategoryId=Guid.NewGuid(),Order=1,CreateTime=DateTime.Now.AddHours(2)},
      15                new Products(){Id=Guid.NewGuid(),Name="美女與野獸",CategoryId=Guid.NewGuid(),Order=1,CreateTime=DateTime.Now},
      16                new Products(){Id=Guid.NewGuid(),Name="世界如此險(xiǎn)惡,您的內(nèi)心需要強(qiáng)大",CategoryId=Guid.NewGuid(),Order=0,CreateTime=DateTime.Now.AddHours(3)}            
      17             };
      18             //查詢語(yǔ)法
      19             var query = from p in pro orderby p.Order, p.CreateTime descending select p;
      20             //方法
      21             var q = pro.OrderBy(p => p.Order).ThenByDescending(p => p.CreateTime);
      22             foreach (var item in query)
      23             {
      24                 Console.WriteLine("name:" + item.Name + "id:" + item.Id + ",order:" + item.Order + ",CreateTime:" + item.CreateTime);
      25             }
      26             foreach (var item in q)
      27             {
      28                 Console.WriteLine("name:" + item.Name + "id:" + item.Id + ",order:" + item.Order + ",CreateTime:" + item.CreateTime);
      29             }
      30 
      31             Console.ReadKey();
      32         }
      33     }
      34 }
      復(fù)制代碼

       運(yùn)行效果:

      五、Reverse

      Reverse將會(huì)把序列中的元素按照從后到前的順序反轉(zhuǎn)。需要注意的是,Reverse方法的返回值是void,例如:

      var q=Employees.Select(e=>e.FirstName).ToList();
      q.Reverse();

      示例:

      復(fù)制代碼
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Linq;
       4 using System.Text;
       5 using System.Threading.Tasks;
       6 
       7 namespace ThenBy
       8 {
       9     class Program
      10     {
      11         static void Main(string[] args)
      12         {
      13             string[] str = { "A", "B", "C", "D", "E"};
      14             var query = str.Select(p => p).ToList();
      15             query.Reverse();
      16             foreach (var item in query)
      17             {
      18                 Console.WriteLine(item);
      19             }
      20 
      21             Console.ReadKey();
      22         }
      23     }
      24 }
      復(fù)制代碼

      運(yùn)行效果:

       

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多