C# Linq 学习笔记

时间:2022-07-12 16:38:13

刚刚学习了 Siki老师 的C#教程Linq部分,以下是笔记

需要引用命名空间

 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 = , Name = "黄蓉", Age = , MenPai = "丐帮", Kongfu = "打狗棒法", Level = },
new MartialArtsMaster() { ID = , Name = "洪七公", Age = , MenPai = "丐帮", Kongfu = "打狗棒法", Level = },
new MartialArtsMaster() { ID = , Name = "郭靖", Age = , MenPai = "丐帮", Kongfu = "降龙十八掌", Level = },
new MartialArtsMaster() { ID = , Name = "任我行", Age = , MenPai = "明教", Kongfu = "葵花宝典", Level = },
new MartialArtsMaster() { ID = , Name = "东方不败", Age = , MenPai = "明教", Kongfu = "葵花宝典", Level = },
new MartialArtsMaster() { ID = , Name = "林平之", Age = , MenPai = "华山", Kongfu = "葵花宝典", Level = },
new MartialArtsMaster() { ID = , Name = "岳不群", Age = , MenPai = "华山", Kongfu = "葵花宝典", Level = },
new MartialArtsMaster() { ID = , Name = "令狐冲", Age = , MenPai = "华山", Kongfu = "独孤九剑", Level = },
new MartialArtsMaster() { ID = , Name = "梅超风", Age = , MenPai = "桃花岛", Kongfu = "九阴真经", Level = },
new MartialArtsMaster() { ID = , Name = "黄药师", Age = , MenPai = "梅花岛", Kongfu = "弹指神功", Level = },
new MartialArtsMaster() { ID = , Name = "风清扬", Age = , MenPai = "华山", Kongfu = "独孤九剑", Level = },
};
List<Kongfu> kongfuList = new List<Kongfu>()
{
new Kongfu() {ID=,Name="打狗棒法",Power= },
new Kongfu() {ID=,Name="降龙十八掌",Power= },
new Kongfu() {ID=,Name="葵花宝典",Power= },
new Kongfu() {ID=,Name="独孤九剑",Power= },
new Kongfu() {ID=,Name="九阴真经",Power= },
new Kongfu() {ID=,Name="弹指神功",Power= }
};

表达式基本写法

var res = from m in masterList
where m.MenPai == "丐帮"//条件,布尔表达式
select m.Name;//返回名字的集合

事实上,条件语句是可以用一个委托代替的,如下面的代码

delegate bool BoolDel(MartialArtsMaster mar);
 var res = from m in masterList
where boolDel(m)
select m.Name;

也可以使用扩展方法的方式进行查询

var res = masterList.Where(new Func<MartialArtsMaster, bool>(boolDel));

接着我用下面的代码输出

 Console.WriteLine("找到 " + res.Count() + " 条记录:");
foreach(var item in res)
{
Console.WriteLine(item);
}

C# Linq 学习笔记

联合查询

使用多个from in 进行查询

 var res = from m in masterList
from k in kongfuList
where m.Kongfu == k.Name && k.Power >
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 > );

C# Linq 学习笔记

使用Join on 进行联合查询

var res = from m in masterList
join k in kongfuList on m.Kongfu equals k.Name
select new { master = m, kongfu = k };

C# Linq 学习笔记

对查询结果进行排序

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);

C# Linq 学习笔记

分组查询

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() };

C# Linq 学习笔记

按照自身字段分组

var res = from m in masterList
group m by m.MenPai into g
select new { MenPai = g.Key, Count = g.Count() };

C# Linq 学习笔记

量词操作

其实就是查询这个集合里是否包含我要查的东西

Console.WriteLine(masterList.Any(m => m.MenPai == "少林寺"));//任一满足,则返回true
Console.WriteLine(masterList.All(m => m.MenPai == "丐帮"));//所有满足,则返回true

第一行查询武林高手列表里有没有少林寺的,第二行查询列表里的是不是都是丐帮的

C# Linq 学习笔记