剛剛學習了 Siki老師 的C#教程Linq部分,以下是筆記 需要引用命名空間 然後我們需要準備數據 武林高手類 武林絕學類 Main方法里的武林高手列表和武林絕學列表 表達式基本寫法 事實上,條件語句是可以用一個委托代替的,如下麵的代碼 也可以使用擴展方法的方式進行查詢 接著我用下麵的代碼輸出 聯 ...
剛剛學習了 Siki老師 的C#教程Linq部分,以下是筆記
需要引用命名空間
1 using System.Linq;
然後我們需要準備數據
武林高手類
/// <summary> /// 武林高手類 /// </summary> class MartialArtsMaster { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string MenPai { get; set; } public string Kongfu { get; set; } public int Level { get; set; } public override string ToString() { return string.Format("{0}--{1}--{2}--{3}--{4}--{5}", ID, Name, Age, MenPai, Kongfu, Level); } }
武林絕學類
class Kongfu { public int ID { get; set; } public string Name { get; set; } public int Power { get; set; } public override string ToString() { return string.Format("{0}--{1}--{2}", ID, Name, Power); } }
Main方法里的武林高手列表和武林絕學列表
List<MartialArtsMaster> masterList = new List<MartialArtsMaster>() { new MartialArtsMaster() { ID = 1, Name = "黃蓉", Age = 18, MenPai = "丐幫", Kongfu = "打狗棒法", Level = 86 }, 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 = 10 }, new MartialArtsMaster() { ID = 5, Name = "東方不敗", Age = 35, MenPai = "明教", Kongfu = "葵花寶典", Level = 540 }, new MartialArtsMaster() { ID = 6, Name = "林平之", Age = 23, MenPai = "華山", Kongfu = "葵花寶典", Level = 404 }, new MartialArtsMaster() { ID = 7, Name = "岳不群", Age = 50, MenPai = "華山", Kongfu = "葵花寶典", Level = 40 }, new MartialArtsMaster() { ID = 8, Name = "令狐沖", Age = 23, MenPai = "華山", Kongfu = "獨孤九劍", Level = 4045 }, new MartialArtsMaster() { ID = 9, Name = "梅超風", Age = 45, MenPai = "桃花島", Kongfu = "九陰真經", Level = 078 }, new MartialArtsMaster() { ID = 10, Name = "黃藥師", Age = 28, MenPai = "梅花島", Kongfu = "彈指神功", Level = 78 }, new MartialArtsMaster() { ID = 11, Name = "風清揚", Age = 46, MenPai = "華山", Kongfu = "獨孤九劍", Level =78 }, }; List<Kongfu> kongfuList = new List<Kongfu>() { new Kongfu() {ID=1,Name="打狗棒法",Power=100 }, new Kongfu() {ID=1,Name="降龍十八掌",Power=45 }, new Kongfu() {ID=1,Name="葵花寶典",Power=75 }, new Kongfu() {ID=1,Name="獨孤九劍",Power=45 }, new Kongfu() {ID=1,Name="九陰真經",Power=39 }, new Kongfu() {ID=1,Name="彈指神功",Power=2 } };
表達式基本寫法
var res = from m in masterList where m.MenPai == "丐幫"//條件,布爾表達式 select m.Name;//返回名字的集合
事實上,條件語句是可以用一個委托代替的,如下麵的代碼
delegate bool BoolDel(MartialArtsMaster mar);
1 var res = from m in masterList 2 where boolDel(m) 3 select m.Name;
也可以使用擴展方法的方式進行查詢
var res = masterList.Where(new Func<MartialArtsMaster, bool>(boolDel));
接著我用下面的代碼輸出
1 Console.WriteLine("找到 " + res.Count() + " 條記錄:"); 2 foreach(var item in res) 3 { 4 Console.WriteLine(item); 5 }
聯合查詢
使用多個from in 進行查詢
1 var res = from m in masterList 2 from k in kongfuList 3 where m.Kongfu == k.Name && k.Power > 90 4 select 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);
使用Join on 進行聯合查詢
var res = from m in masterList join k in kongfuList on m.Kongfu equals k.Name select new { master = m, kongfu = k };
對查詢結果進行排序
var res = from m in masterList orderby m.Level, m.Age descending //級別升序,年齡降序 select m;
擴展方法形式
var res = masterList.OrderBy(m => m.Level).ThenByDescending(m => m.Age);
分組查詢
var res = from k in kongfuList join m in masterList on k.Name equals m.Kongfu into groups//根據 k 分組 orderby groups.Count() select new { kongfu = k, count = groups.Count() };
按照自身欄位分組
var res = from m in masterList group m by m.MenPai into g select new { MenPai = g.Key, Count = g.Count() };
量詞操作
其實就是查詢這個集合里是否包含我要查的東西
Console.WriteLine(masterList.Any(m => m.MenPai == "少林寺"));//任一滿足,則返回true Console.WriteLine(masterList.All(m => m.MenPai == "丐幫"));//所有滿足,則返回true
第一行查詢武林高手列表裡有沒有少林寺的,第二行查詢列表裡的是不是都是丐幫的