刚刚学习了 Siki老师 的C#教程Linq部分,以下是笔记
需要引用命名空间
1 using System.Linq;
然后我们需要准备数据
武林高手类
////// 武林高手类 /// 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方法里的武林高手列表和武林绝学列表
ListmasterList = new List () { 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 kongfuList = new List () { 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 Funcbool>(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
第一行查询武林高手列表里有没有少林寺的,第二行查询列表里的是不是都是丐帮的