1 class Master 2 { 3 publicintId{ get;set;} 4 publicstringName{ get;set;} 5 publicintAge{ get;set;} 6 publicstringKongfu{ get;set;} 7 publicstringMenPai{ get;set;} 8 publicintLevel{ get;set;} 9 public overide string ToString() 10 { 11 return string.Fromat("Id:{0},Name:{1},Age:{2},Kongfu:{3},MenPai:{4},Level:{5}", Id, Name, Age, Kongfu, Menpai, Leve) 12 } 13 }
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 }
1 static void Main(String[] args) 2 { 3 var master =newList<Master>() 4 { 5 newMaster(){Id=1,Name="黃蓉",Age=18,Menpai="丐幫", Kongfu="打狗棒法",Level=9}, 6 newMaster(){Id=2,Name="洪七公",Age=70,Menpai="丐幫", Kongfu="打狗棒法",Level=10}, 7 newMaster(){Id=3,Name="郭靖",Age=22,Menpai="丐幫", Kongfu="降龍十八掌",Level=10}, 8 newMaster(){Id=4,Name="任我行",Age=50,Menpai="明教", Kongfu="葵花寶典",Level=1}, 9 newMaster(){Id=5,Name="東方不敗",Age=35,Menpai="明教", Kongfu="葵花寶典", Level=10}, 10 newMaster(){Id=6,Name="林平之",Age=23,Menpai="華山", Kongfu="葵花寶典", Level=7}, 11 newMaster(){Id=7,Name="岳不群", Age=50,Menpai="華山",Kongfu="葵花寶典",Level=8}, 12 newMaster(){Id=8,Name="令狐沖",Age=23,Menpai="華山",Kongfu="獨孤九劍", Level=10}, 13 newMaster(){Id=9,Name="梅超風",Age=23,Menpai="梅花島",Kongfu="九陰真經", Level=10}, 14 newMaster(){Id=10,Name="黃藥師",Age=23,Menpai="梅花島",Kongfu="彈指神功",Level=10}, 15 newMaster(){ Id = 11, Name = "風清揚", Age = 23, Menpai = "華山", Kongfu = "獨孤九劍", Level = 10 } 16 }; 17 var kongfu =newList<KongFu>() 18 { 19 newKongFu(){Id=1,Name="打狗棒法", Power=95 }, 20 newKongFu(){Id=2,Name="降龍十八掌",Power=100}, 21 newKongFu(){Id=3,Name="葵花寶典", Power=100}, 22 newKongFu(){Id=4,Name="獨孤九劍", Power=100}, 23 newKongFu(){Id=5,Name="九陰真經", Power=100}, 24 newKongFu(){Id=6,Name="彈指神功", Power=100}, 25 }; 26 }
【第一種】:找出Level大於8的列表來 var result = new List<Master>(); foreach (var item in master) { if (item.Level > 8) { result.Add(item); } } foreach (var item in result) { Console.WriteLine(item.ToString());//ToString()不寫默認存在 } Console.ReadKey(); //需要在 master類和KongFu中創建 ToString()方法,因為在查詢完以後進行遍歷(foreach)的時候h會出現以下錯誤 【第二種】:利用專業查詢語言查找並排序 //from:表示從哪個集合裡查找……where:查新條件……select:返回結果列表 var result2 = from m in master where m.Level < 10//查詢條件 orderby m.Level//根據level進行升序排序 orderby m.Level descending//根據level進行降序排序 select m; foreach (var item in result2) { Console.WriteLine(item); } Console.ReadKey(); 【按條件查找並排序】 var ressult22 = from m in master.Where(m => m.Level < 10).OrderBy(m => m.Age)//OrderBy && OrderByDescending 升序和降序 排列 select m; foreach (var item in ressult22) { Console.WriteLine(item); } Console.ReadKey(); 【第三種】:調用函數的方法來實現查找功能 var result3 = master.Where(T1); foreach (var item in result3) { Console.WriteLine(item); } Console.ReadKey(); //需要調用的類 static bool T1(Master m) { if (m.Level < 10) { return true; } else { return false; } } 【第四種】:利用lamada表達式查找 var result4 = master.Where(m => m.Level < 10);//lamada表達式 參數名=>表達式 foreach (var item in result4) { Console.WriteLine(item); } Console.ReadKey(); 【第五種】:將兩個列表聯合起來,形成 Count(master) * Count(Kongfu) 的列表 var result5 = from m in master from f in kongfu select new { mm = m, ff = f };// foreach (var item in result5) { Console.WriteLine(item); } Console.ReadKey()來自為知筆記(Wiz)
【第六種】:查詢當 master.Level == Kongfu.Id,時兩個的信息 var result6 = from m in master from f in kongfu //where m.Level == f.Id where m.Kongfu == f.Name && f.Power > 90 select new { mm = m, ff = f }; foreach (var item in result6) { Console.WriteLine(item); } Console.ReadKey();
【第七種】:實現master和Kongfu中屬性相等的信息輸出 var result7 = from m in master //將kongfu列表中的信息放入k中,選出 m.Kongfu == k.Name的信息 join k in kongfu on m.Kongfu equals k.Name select new { mm = m, kk = k }; foreach (var item in result7) { Console.WriteLine(item); } Console.ReadKey(); 【第八種】:一人修煉一門功夫,一門功夫被多少人修煉 var result8 = from k in kongfu join m in master on k.Name equals m.Kongfu into groups//把武林高手分類,看看哪門功夫被人修煉的多 orderby groups.Count()//按照修煉人進行 select new { kongfu = k, count = groups.Count() };//groups.count:獲取到 foreach (var item in result8) { Console.WriteLine(item); } Console.ReadKey();
//將修煉功夫一樣的人放在一起並輸出master和kongfu的信息 var result = from k in kongfu join m in master on k.Name equals m.Kongfu select new { mm = m, kk = k }; foreach (var item in result) { Console.WriteLine(item); } Console.ReadKey();
【第九種】:按照自身幫派進行分組, var result9 = from m in master group m by m.Menpai//將門派分組,放到m中 into p//表示放到p中 select new { count = p.Count(), key = p.Key };//count = p.Count()表示這組有多少人 //key = p.Key表示這個是按照什麼分的,即關鍵字"Key"為Menpai foreach (var item in result9) { Console.WriteLine(item); } Console.ReadKey(); 【第十種】:確定序列中元素是否滿足條件 Any在內部循環遍歷每個元素 根據m=>m.Menpai判斷是否滿足條件 bool result10 = master.Any(m => m.Menpai == "丐幫"); Console.WriteLine(result10); Console.ReadKey();