Linq的篩選、排序等幾種用法(VS2010)


1.首先需要一個集合用來測試:

 //定義一個集合要用的類:

 public class People
  {
            public People(string name, string sex, int age,decimal height)
            {
                this.Name = name;
                this.Sex = sex;
                this.Age = age;
                this.Height = height;
            }

            public string Name { get; set; }
            public string Sex { get; set; }
            public int Age { get; set; }

            public decimal Height { get; set; }

            public string ToString(string format)
            {
                return ToString(format);
            }

}

 //制造幾條測試數據:

 List<People> list = new List<People>();
 list.Add(new People("ymw", "男", 22, 170m));
 list.Add(new People("Lily", "女", 21, 160m));
 list.Add(new People("xj", "女", 20, 158m));
 list.Add(new People("Lucy", "女", 23, 166m));
 list.Add(new People("MM", "女", 18, 168m));

 

 2.Linq特性示例一:

            //篩選
            List<People> lists = list.FindAll(
                 delegate(People p)
                 {
                     return p.Sex == "女";
                 });

             //排序(根據類型)
             lists.Sort(
                 delegate(People p1, People p2)
                 {
                     return p1.Name.CompareTo(p2.Name);
                 });

             foreach (People item in lists)
             {
                 this.txt.Text += item.Name + "~" + item.Age + "~" + item.Sex+"~"+item.Height + "\r\n";
             }

 //如圖測試結果:過濾了性別為男的人並按照名稱排序↓

 

3.Linq特性示例二:

 //Where、Select、OrderByDescending 用法:Where、Select用來帥選過濾;OrderByDescending是按降序排列

 IEnumerable<People> lists = list.Where(
                delegate(People p)
                {
                    return p.Sex == "女";
                }).OrderByDescending( //OrderBy
                delegate(People p)
                {
                    return p.Height;
                }).Select(
                delegate(People p)
                {
                    return p;
                });
            foreach (People item in lists)
            {
                this.txt.Text += item.Name + "~" + item.Age + "~" + item.Sex + "~" + item.Height + "\r\n";
            }

// var lists2 = list.Where(p => p.Sex == "女" || p.Sex == "男").OrderBy(p => p.Height).Select(p => p);

//如圖測試結果:過濾了性別為男的人並按照身高降序排序↓

4.Linq特性示例三:

// λ表達式
 IEnumerable<People> lists = list.Where(p => p.Sex == "女").OrderByDescending(p => p.Height).Select(p => p);

 foreach (People item in lists)
  {
         this.txt.Text += item.Name + "~" + item.Age + "~" + item.Sex + "~" + item.Height + "\r\n";
  }

//測試結果如上圖實例二:過濾了性別為男的人並按照身高降序排序↓

 

5.Linq特性示例四:

var lists = (from p in list
                         where p.Sex == "女"
                         orderby p.Age ascending, p.Height
                         select p).Take(4); //Take()取前幾條記錄

  foreach (People item in lists)
  {
          this.txt.Text += item.Name + "~" + item.Age + "~" + item.Sex + "~" + item.Height + "\r\n";
   }

//測試結果如下圖,按年齡排序且取前四條記錄↓


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM