LINQ基础篇(上)
LINQ
Language Integrated Query 语言集成查询
举个例子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 int[] scores = new int[] { 65,50,45,90,87,65}; 2 Console.WriteLine("高分有:"); 3 //方法一 4 var highscore = from score in scores 5 where score > 80 6 select score; 7 var highscore2 = scores.Where(score => score > 80); 8 foreach (var item in highscore) 9 { 10 Console.WriteLine(item); 11 } 12 //方法二 13 for (int i = 0; i < scores.Length; i++) 14 { 15 if (scores[i]>80) 16 { 17 Console.WriteLine(scores[i]); 18 } 19 } 20 Console.ReadKey();
代码分析:
1 方法一和方法二都对分数数组进行处理,方法二是一般的用For循环,方法一是查询表达式和方法表达式两种方式的LINQ
2 方法一中的方法表达式 var highscore2 = scores.Where(score => score > 80); 运用了Lambda表达式,形式更为简洁
3 方法一中的查询表达式 var highscore = from score in scores where score > 80 select score;
与正常的SQL语句有一点类似,有关键字select from where等
4 通过例子可以看出LINQ是对数据的处理。
关键字讲解
from in
作用:引用数据源,比如数组、集合、数据库的表、DataTable
语法:from 别名 in 数据源
where
作用:条件过滤 多个条件时用&& ||
Select
作用:表达式需返回的值
语法:单一返回 select 需返回的 多个返回 select new {需返回的A,需返回的B}
group by into
作用:根据分类依据对数据进行分类
语法: 单一分类 group 别名A by 别名A.字段 into 别名B
多个分类 group 别名A by new {别名A.字段…} into 别名B
orderby ascending descending
作用:查询结果排序
Join on equals into
作用:数据源连接,左右连接,内连接
语法:from 别名1 in 数据源1 join 别名2 in 数据源2 on 别名1.字段 equals 别名2.字段 into 别名3
代码例子
定义Student类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Student 2 { 3 public string StudentNo { get; set; } 4 public string Name { get; set; } 5 public int Age { get; set; } 6 public string Sex { get; set; } 7 public string ClassNo { get; set; } 8 }
定义ClassInfo类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class ClassInfo 2 { 3 public string ClassNo { get; set; } 4 public string ClassName { get; set; } 5 public string HeadTeacher { get; set; } 6 }
调用方式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 List<Student> listStudent = new List<Student> 2 { 3 new Student { StudentNo="1",Name="张三",Age=18,Sex="男",ClassNo="1" }, 4 new Student { StudentNo="2",Name="李四",Age=19,Sex="男",ClassNo="1" }, 5 new Student { StudentNo="3",Name="王五",Age=18,Sex="男",ClassNo="1" }, 6 new Student { StudentNo="4",Name="张珊",Age=17,Sex="女",ClassNo="2" }, 7 new Student { StudentNo="5",Name="王小明",Age=20,Sex="男",ClassNo="1" }, 8 new Student { StudentNo="6",Name="刘晓",Age=18,Sex="女",ClassNo="1" }, 9 new Student { StudentNo="7",Name="赵尚武",Age=19,Sex="女",ClassNo="1" }, 10 new Student { StudentNo="8",Name="关翊",Age=20,Sex="女",ClassNo="2" }, 11 new Student { StudentNo="9",Name="吴才",Age=17,Sex="男",ClassNo="2" }, 12 13 }; 14 List<ClassInfo> listClassInfo = new List<ClassInfo> 15 { 16 new ClassInfo { ClassNo="1",ClassName="育才一班",HeadTeacher="王一晟" }, 17 new ClassInfo { ClassNo="2",ClassName="育才二班",HeadTeacher="张天天"}, 18 }; 19 var p = from s in listStudent 20 group s by new { s.ClassNo, s.Sex } into g 21 join c in listClassInfo on g.Key.ClassNo equals c.ClassNo 22 orderby g.Count() descending, g.Key.Sex descending 23 select new { sex=g.Key.Sex, classname=c.ClassName,headteacher=c.HeadTeacher, Count = g.Count() }; 24 var lp = listStudent.GroupBy(s => new { s.ClassNo, s.Sex}) 25 .Join(listClassInfo, g => g.Key.ClassNo, c => c.ClassNo, (g, c) => new { c.ClassName, c.HeadTeacher, g.Key.Sex,count=g.Count() }) 26 .OrderByDescending(h =>h.count) 27 .ThenByDescending(h => h.Sex) 28 .Select(h=>new { sex = h.Sex, classname = h.ClassName, headteacher = h.HeadTeacher, Count = h.count }); 29 foreach (var item in lp) 30 { 31 Console.WriteLine("班级:{0},班主任:{1},性别:{2},人数{3}", item.classname,item.headteacher, item.sex, item.Count); 32 } 33 Console.ReadKey();
输出
注意 此处group by应该在join之前,与SQL语句的刚好相反,原因是先join再group by导
致listClassInfo中的字段无法访问,需要嵌套foreach来输出