C#LINQ 查询学习笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#LINQ 查询学习笔记相关的知识,希望对你有一定的参考价值。
【LINQ 查询】
2016-10-24
事例:
先定义两个类Master和KongFu,然后在这两个类中定义变量
-
1 class Master 2 { 3 publicintId{ get;set;} 4 publicstringName{ get;set;} 5 publicintAge{ get;set;} 6 publicstringKongfu{ get;set;} 7 publicstringMenPai{ get;set;} 8 publicintLevel{ get;set;} 9 public overide string ToString() 10 { 11 return string.Fromat("Id:{0},Name:{1},Age:{2},Kongfu:{3},MenPai:{4},Level:{5}", Id, Name, Age, Kongfu, Menpai, Leve) 12 } 13 }
1 class KongFu 2 { 3 public int Id { get;set;} 4 public string Name { get;set;} 5 public int Power { get;set;} 6 7 public override string ToString() 8 { 9 return string.Format("Id:{0},Name:{1},Power:{2}", Id, Name, Power); 10 } 11 }
1 static void Main(String[] args) 2 { 3 var master =newList<Master>() 4 { 5 newMaster(){Id=1,Name="黄蓉",Age=18,Menpai="丐帮", Kongfu="打狗棒法",Level=9}, 6 newMaster(){Id=2,Name="洪七公",Age=70,Menpai="丐帮", Kongfu="打狗棒法",Level=10}, 7 newMaster(){Id=3,Name="郭靖",Age=22,Menpai="丐帮", Kongfu="降龙十八掌",Level=10}, 8 newMaster(){Id=4,Name="任我行",Age=50,Menpai="明教", Kongfu="葵花宝典",Level=1}, 9 newMaster(){Id=5,Name="东方不败",Age=35,Menpai="明教", Kongfu="葵花宝典", Level=10}, 10 newMaster(){Id=6,Name="林平之",Age=23,Menpai="华山", Kongfu="葵花宝典", Level=7}, 11 newMaster(){Id=7,Name="岳不群", Age=50,Menpai="华山",Kongfu="葵花宝典",Level=8}, 12 newMaster(){Id=8,Name="令狐冲",Age=23,Menpai="华山",Kongfu="独孤九剑", Level=10}, 13 newMaster(){Id=9,Name="梅超风",Age=23,Menpai="梅花岛",Kongfu="九阴真经", Level=10}, 14 newMaster(){Id=10,Name="黄药师",Age=23,Menpai="梅花岛",Kongfu="弹指神功",Level=10}, 15 newMaster(){ Id = 11, Name = "风清扬", Age = 23, Menpai = "华山", Kongfu = "独孤九剑", Level = 10 } 16 }; 17 var kongfu =newList<KongFu>() 18 { 19 newKongFu(){Id=1,Name="打狗棒法", Power=95 }, 20 newKongFu(){Id=2,Name="降龙十八掌",Power=100}, 21 newKongFu(){Id=3,Name="葵花宝典", Power=100}, 22 newKongFu(){Id=4,Name="独孤九剑", Power=100}, 23 newKongFu(){Id=5,Name="九阴真经", Power=100}, 24 newKongFu(){Id=6,Name="弹指神功", Power=100}, 25 }; 26 }
开始建立查询:
【第一种】:找出Level大于8的列表来 var result = new List<Master>(); foreach (var item in master) { if (item.Level > 8) { result.Add(item); } } foreach (var item in result) { Console.WriteLine(item.ToString());//ToString()不写默认存在 } Console.ReadKey(); //需要在 master类和KongFu中创建 ToString()方法,因为在查询完以后进行遍历(foreach)的时候h会出现以下错误 【第二种】:利用专业查询语言查找并排序 //from:表示从哪个集合里查找……where:查新条件……select:返回结果列表 var result2 = from m in master where m.Level < 10//查询条件 orderby m.Level//根据level进行升序排序 orderby m.Level descending//根据level进行降序排序 select m; foreach (var item in result2) { Console.WriteLine(item); } Console.ReadKey(); 【按条件查找并排序】 var ressult22 = from m in master.Where(m => m.Level < 10).OrderBy(m => m.Age)//OrderBy && OrderByDescending 升序和降序 排列 select m; foreach (var item in ressult22) { Console.WriteLine(item); } Console.ReadKey(); 【第三种】:调用函数的方法来实现查找功能 var result3 = master.Where(T1); foreach (var item in result3) { Console.WriteLine(item); } Console.ReadKey(); //需要调用的类 static bool T1(Master m) { if (m.Level < 10) { return true; } else { return false; } } 【第四种】:利用lamada表达式查找 var result4 = master.Where(m => m.Level < 10);//lamada表达式 参数名=>表达式 foreach (var item in result4) { Console.WriteLine(item); } Console.ReadKey(); 【第五种】:将两个列表联合起来,形成 Count(master) * Count(Kongfu) 的列表 var result5 = from m in master from f in kongfu select new { mm = m, ff = f };// foreach (var item in result5) { Console.WriteLine(item); } Console.ReadKey()
【第六种】:查询当 master.Level == Kongfu.Id,时两个的信息 var result6 = from m in master from f in kongfu //where m.Level == f.Id where m.Kongfu == f.Name && f.Power > 90 select new { mm = m, ff = f }; foreach (var item in result6) { Console.WriteLine(item); } Console.ReadKey();
【第七种】:实现master和Kongfu中属性相等的信息输出 var result7 = from m in master //将kongfu列表中的信息放入k中,选出 m.Kongfu == k.Name的信息 join k in kongfu on m.Kongfu equals k.Name select new { mm = m, kk = k }; foreach (var item in result7) { Console.WriteLine(item); } Console.ReadKey(); 【第八种】:一人修炼一门功夫,一门功夫被多少人修炼 var result8 = from k in kongfu join m in master on k.Name equals m.Kongfu into groups//把武林高手分类,看看哪门功夫被人修炼的多 orderby groups.Count()//按照修炼人进行 select new { kongfu = k, count = groups.Count() };//groups.count:获取到 foreach (var item in result8) { Console.WriteLine(item); } Console.ReadKey();
//将修炼功夫一样的人放在一起并输出master和kongfu的信息 var result = from k in kongfu join m in master on k.Name equals m.Kongfu select new { mm = m, kk = k }; foreach (var item in result) { Console.WriteLine(item); } Console.ReadKey();
【第九种】:按照自身帮派进行分组, var result9 = from m in master group m by m.Menpai//将门派分组,放到m中 into p//表示放到p中 select new { count = p.Count(), key = p.Key };//count = p.Count()表示这组有多少人 //key = p.Key表示这个是按照什么分的,即关键字"Key"为Menpai foreach (var item in result9) { Console.WriteLine(item); } Console.ReadKey(); 【第十种】:确定序列中元素是否满足条件 Any在内部循环遍历每个元素 根据m=>m.Menpai判断是否满足条件 bool result10 = master.Any(m => m.Menpai == "丐帮"); Console.WriteLine(result10); Console.ReadKey();
以上是关于C#LINQ 查询学习笔记的主要内容,如果未能解决你的问题,请参考以下文章