Linq联合查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linq联合查询相关的知识,希望对你有一定的参考价值。
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联合查询的主要内容,如果未能解决你的问题,请参考以下文章