首先定义两个类:武林高手类和功夫类
并且对这两个类重写ToString()方法,让输出更加明确
/// <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("Id:{0},Name:{1},Age:{2},Menpai:{3},Kongfu:{4},level:{5}", Id, Name, Age, Menpai, Kongfu, level);
}
}
/// <summary>
/// 功夫类
/// </summary>
class Kongfu
{
public int Id { get; set; }
public string Name { get; set; }
public int Power { get; set; }
public override string ToString()
{
return string.Format("Id:{0},Name:{1},Power:{2}", Id, Name, Power);
}
}
有了两个类,在创建两个List用来存储一些要用到信息
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 }
};
用LINQ进行查询:查询武林高手中,武学级别大于8的人
方法一:使用for循环查询
var result = new List<MartialArtsMaster>();//定义一个List来存储结果
foreach (var temp in masterList)
{
if (temp.level > 8)
result.Add(temp);//把符合要求的加入List中
}
foreach(var temp in result)
{
Console.WriteLine(temp.ToString());
}
Console.WriteLine();
方法二:使用LINQ表达式写法:
var res = from m in masterList //from后面设置查询的集合,表示从那个集合做查询,m是集合里面的每个元素
where m.level > 8 //where后面跟上查询的条件
select m; //表示把满足条件的结果集合返回
var name = from n in masterList
where n.level > 8
select n.Name; //只输出name
foreach(var temp in res)
{
Console.WriteLine(temp.ToString());
}
Console.WriteLine();
方法三:使用扩展方法:
static bool Test1(MartialArtsMaster master)
{
if (master.level > 8) return true;
return false;
}
var res1 = masterList.Where(Test1);//Where方法需要传递一个委托,会把masterList中的每个元素传进去进行过滤
foreach (var temp in res1)
{
Console.WriteLine(temp.ToString());
}
//因为传进去的委托其实只有一句话,我们可以使用lambda表达式来实现
var res2 = masterList.Where(m => m.level > 8);
foreach (var temp in res2)
{
Console.WriteLine(temp.ToString());
}
Console.WriteLine();
LINQ联合查询,类似于多表查询
表达式写法
//如果这么写,生成11*6条记录的每一条进行结合
//就相当于就会把masterList和kongfuList中的每条集合又进行了一次一一组合
//生成了一个由m in masterList 和 k in kongfuList组成的List resLianHe
var resLianHe = from m in masterList
from k in kongfuList
where m.Kongfu == k.Name && k.Power > 90 //加入限定条件,
select new { master = m, kongfu = k };
foreach (var temp in resLianHe)
{
Console.WriteLine(temp);//这里会输出66条"武林高手信息,武学信息"这样的信息
}
Console.WriteLine();
扩展方法
//LINQ联合查询,扩展方法写法
var res3 = 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 res3)
{
Console.WriteLine(temp.ToString());
}
Console.WriteLine();
对查询结果进行排序
表达式写法
var res4 = from m in masterList
where m.level > 8 && m.Menpai == "丐帮"
//按照年龄降序,(默认是升序),如果是多个属性,按顺序来
//如果第一个属性相同,就比较第二个属性
orderby m.Age descending
select m;
foreach(var temp in res4)
{
Console.WriteLine(temp);
}
Console.WriteLine();
扩展方法
//var res4Method = masterList.Where(m => m.level > 8 && m.Menpai == "丐帮").OrderBy(m => m.Age);
//如果按照多个属性进行排列,不能连续性的使用OrderBy,否则会在后面的OrderBy进行重新排序,即之前的排序会乱掉
//需要使用ThenBy
var res4Method = masterList.Where(m => m.level > 8 && m.Menpai == "丐帮").OrderBy(m => m.level).ThenBy(m=>m.Age);
foreach (var temp in res4Method)
{
Console.WriteLine(temp);
}
Console.WriteLine();
Join on 集合联合
var res5 = from m in masterList
join k in kongfuList on m.Kongfu equals k.Name//注意,比较用equals,不能用==
where k.Power>90
orderby m.Age
select new { master = m, kongfu = k };
foreach (var temp in res5)
{
Console.WriteLine(temp);
}
Console.WriteLine();
分组查询
把两个集合进行分组
//把两个集合进行分组
var res6 = from k in kongfuList
join m in masterList on k.Name equals m.Kongfu
//select new { kongfu = k, master = m };//如果用了into groups就要用组进行操作了
into groups
orderby groups.Count() descending
select new { konfu = k, count = groups.Count() };
foreach (var temp in res6)
{
Console.WriteLine(temp);
}
Console.WriteLine();
单个集合按照自身字段分组 group by into
var res7 = from m in masterList
group m by m.Menpai into g//g是自己定义的
orderby g.Count() descending
select new { count = g.Count(), key = g.Key };//g.Key表示Key按照哪个属性分得组
foreach (var temp in res7)
{
Console.WriteLine(temp);
}
Console.WriteLine();
量词操作符
//Any 方法 返回bool类型值,只要有一个满足条件就返回true
bool res8 = masterList.Any(m => m.Menpai == "丐帮");
//All 方法 判断集合里面是否全部满足某一个条件
bool res9 = masterList.All(m => m.Menpai == "丐帮");