Linq聯合查詢
阿新 • • 發佈:2017-07-15
turn 成員 級別 sta 說明 ole 一個 對象 color
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQ01 { class Program { static void Main(string[] args) { //初始化武林高手 var masterList = new List<MartialArtsMaster>() {new MartialArtsMaster(){ Id = 1, Name = "黃蓉", Age = 18, Menpai = "丐幫", Kongfu = "打狗棒法", Level = 9 }, new MartialArtsMaster(){ Id = 2, Name = "洪七公", Age = 70, Menpai = "丐幫", Kongfu = "打狗棒法", Level = 10 }, new MartialArtsMaster(){ Id = 3, Name = "郭靖", Age = 22, Menpai = "丐幫", Kongfu = "降龍十八掌",Level = 10 }, new MartialArtsMaster(){ Id = 4, Name = "任我行", Age = 50, Menpai = "明教", Kongfu = "葵花寶典", Level = 1 }, new MartialArtsMaster(){ Id = 5, Name = "東方不敗",Age = 35, Menpai = "明教", Kongfu = "葵花寶典", Level = 10 }, new MartialArtsMaster(){ Id = 6, Name = "林平之", Age = 23, Menpai = "華山", Kongfu = "葵花寶典", Level = 7 }, new MartialArtsMaster(){ Id = 7, Name = "嶽不群", Age = 50, Menpai = "華山", Kongfu = "葵花寶典", Level = 8 }, new MartialArtsMaster() { Id = 8, Name = "令狐沖", Age = 23, Menpai = "華山", Kongfu = "獨孤九劍", Level = 10 }, new MartialArtsMaster() { Id = 9, Name = "梅超風", Age = 23, Menpai = "桃花島", Kongfu = "九陰真經", Level = 8 }, new MartialArtsMaster() { Id =10, Name = "黃藥師", Age = 23, Menpai = "梅花島", Kongfu = "彈指神通", Level = 10 }, new MartialArtsMaster() { Id = 11, Name = "風清揚", Age = 23, Menpai = "華山", Kongfu = "獨孤九劍", Level = 10 } }; //初始化武學 var kongfuList = new List<Kongfu>() { new Kongfu(){Id=1, Name="打狗棒法", Power=90}, new Kongfu(){Id=2, Name="降龍十八掌",Power=95}, new Kongfu(){Id=3, Name="葵花寶典", Power=100}, new Kongfu(){Id=4, Name="獨孤九劍", Power = 100 }, new Kongfu(){Id=5, Name="九陰真經", Power = 100 }, new Kongfu(){Id=6, Name="彈指神通", Power = 100 } }; //方法一:查詢所有開學級別大於8的高手 //var res = new List<MartialArtsMaster>(); //foreach (var temp in masterList) //{ // if (temp.Level > 8) // { // res.Add(temp); // } //} //方法二:使用linq查尋(表達式與法) //var res = from m in masterList//from後面設置查詢的集合 // where m.Level > 8 && m.Menpai == "丐幫"//條件語句可以用&&或|| // select m.Name;//把m的集合返回 //方法三:擴展方法的寫法.where(過濾方法),意思就是這個方法會遍列自身的所有成員並當作參數分別傳入過濾方法,在過濾方法中進行所要的判斷。 //var res = masterList.Where(Test1);// //方法四:一般直接使用lamda表達式 //var res = masterList.Where(m=>m.Level>8 && m.Menpai== "丐幫"); //運用一:聯合查詢(表達式法) //查詢Powe>90的Naster //var res = from m in masterList // from k in kongfuList // where m.Kongfu==k.Name && k.Power > 90 // select new { master=m,kongfu=k}; //運用二:聯合查詢(擴展方法法) //說明:m => kongfuList是 masterList.SelectMany另一個查詢對象, //(m, k) => new { master = m, Kongfu = k }是將兩個欲查詢的對象用lamda表達式建立一個新的new { master = m, Kongfu = k }集合,這裏邊的就是兩個查詢對象的合集(from m in masterList from k in kongfuList) // Where(x=>x.master.Kongfu==x.Kongfu.Name);就是條例語句 //var res = masterList.SelectMany(m => kongfuList, (m, k) => new { master = m, Kongfu = k }); var res = masterList.SelectMany(m => kongfuList, (m, k) => new { master = m, Kongfu = k }).Where(x=>x.master.Kongfu==x.Kongfu.Name &&x.Kongfu.Power>90); foreach (var temp in res) { Console.WriteLine(temp.ToString()); } Console.WriteLine(res.Count()); Console.ReadKey(); } //過濾方法 static bool Test1(MartialArtsMaster master) { if (master.Level > 8 && master.Menpai == "丐幫") return true; return false; } } }
Linq聯合查詢