1. 自定義 Master,Kongfu 類 2. 測試代碼 ...
1. 自定義 Master,Kongfu 類
1 class Master 2 { 3 4 public int Id { get; set; } 5 public string Name { get; set; } 6 public int Age { get; set; } 7 public string MenPai { get; set; } 8 public string Kongfu { get; set; } 9 public int Level { get; set; } 10 11 public override string ToString() 12 { 13 return string.Format("Id:{0},Name:{1},Age:{2},MenPai:{3},Kongfu:{4},Level:{5}", Id, Name, Age,MenPai,Kongfu,Level); 14 } 15 }
1 class Kongfu 2 { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 public int Power { get; set; } 6 7 public override string ToString() 8 { 9 return string.Format("Id:{0},Name:{1},Power:{2}", Id, Name, Power); 10 } 11 }
2. 測試代碼
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 var master = new List<Master>() 6 { 7 new Master() { Id = 100, Age = 100, Kongfu = "九陽神功",Level=10,MenPai="華山",Name="李二狗" }, 8 new Master() { Id = 50, Age = 10, Kongfu = "九陽神功1" ,Level=5,MenPai="華山",Name="李大狗"}, 9 new Master() { Id = 200, Age = 60, Kongfu = "九陽神功2",Level=6,MenPai="華山",Name="李狗蛋" }, 10 }; 11 12 var kongfu = new List<Kongfu>() 13 { 14 new Kongfu(){Id=1,Name="打狗棒法",Power=90}, 15 new Kongfu(){Id=2,Name="打狗棒法1",Power=30}, 16 new Kongfu(){Id=3,Name="打狗棒法2",Power=20}, 17 }; 18 19 //使用LINQ做查詢(表達式寫法) 20 var res1 = from m in master //from後面設置查詢的集合 21 where m.Level > 8 //where跟上條件 22 23 select m;//表示m的結果 24 //select m.Name; //只輸出名字 25 26 27 //多個條件 28 var res3 = from m in master 29 30 where m.Level > 8 && m.MenPai == "丐幫" 31 32 select m; 33 34 var res4 = master.Where(m => m.Level > 8 && m.MenPai == "丐幫"); 35 36 //LINQ聯合查詢 37 var res5 = from m in master 38 from k in kongfu 39 where m.Kongfu==k.Name 40 select new { master = m, kongfu = k }; 41 //select m; 42 foreach (var temp in res) 43 { 44 Console.WriteLine(temp); 45 } 46 47 //join on 聯合查詢 48 var res12 = from m in master 49 join k in kongfu on m.Kongfu equals k.Name 50 where k.Power>90 51 select new { master = m, kongfu = k }; 52 53 54 //對查詢結果升序排序 55 var res7 = from m in master 56 57 where m.Level > 8 && m.MenPai == "丐幫" 58 orderby m.Age 59 60 select m; 61 62 //對查詢結果倒序排序 63 var res8 = from m in master 64 65 where m.Level > 8 && m.MenPai == "丐幫" 66 orderby m.Age descending 67 68 select m; 69 70 //對查詢結果升序排序 71 var res9 = from m in master 72 73 where m.Level > 8 && m.MenPai == "丐幫" 74 orderby m.Age,m.Level //按照多個欄位進行排序,如果欄位的屬性相同,就按照第二個屬性排序 75 76 select m; 77 78 var res10 = master.Where(m => m.Level > 8 && m.MenPai == "丐幫").OrderBy(m => m.Age); 79 var res11 = master.Where(m => m.Level > 8 && m.MenPai == "丐幫").OrderBy(m => m.Age).ThenBy(m=>m.Level); 80 81 82 83 //把人物按照所學功夫進行分類,看下哪個功夫修煉人數最多(分組查詢) 84 var res13 = from k in kongfu 85 join m in master on k.Name equals m.Kongfu 86 into groups 87 orderby groups.Count() 88 select new { kongfu = k,count=groups.Count()}; 89 90 //按照欄位分組 91 var res14 = from m in master 92 group m by m.MenPai 93 into g 94 select new { count=g.Count(),key=g.Key}; //g.Key按照哪個屬性分組 95 96 //量詞操作符any all判斷集合中是否滿足某個條件 97 bool res15=master.Any(m => m.MenPai == "丐幫"); 98 Console.WriteLine(res15);//只要有一個滿足返回true 99 100 bool res16=master.All(m => m.MenPai == "丐幫");//所有都滿足才返回true 101 102 103 104 Console.ReadKey(); 105 } 106 107 //過濾方法 108 static bool Test1(Master m) 109 { 110 if (m.Level > 8) return true; 111 return false; 112 } 113 }