linq的简单使用
Posted liangshibo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linq的简单使用相关的知识,希望对你有一定的参考价值。
1、linq是什么
linq是c#语言的一个扩展,可以将数据查询直接集成到编程语言本身中。目的就是提供了一种可移式的、一致的方式,来查询 、排序和分组许多不同种类的数据(XML、JSON、SQL数据库、对象集合、Web服务、企业目录等)。
2、设置数据源
private class Student { public string Name { get; set; } public string Subject { get; set; } public int Scores { get; set; } }
上面的代码定义对象类型,下面代码声明对象列表并生成数据,后面linq查询使用的数据源默认都是此数据源。
var studentList = new List<Student>(); studentList.Add(new Student() { Name = "张三", Subject = "数学", Scores = 28 }); studentList.Add(new Student() { Name = "张三", Subject = "语文", Scores = 43 }); studentList.Add(new Student() { Name = "张三", Subject = "英语", Scores = 87 }); studentList.Add(new Student() { Name = "李四", Subject = "数学", Scores = 45 }); studentList.Add(new Student() { Name = "李四", Subject = "语文", Scores = 56 }); studentList.Add(new Student() { Name = "李四", Subject = "英语", Scores = 28 }); studentList.Add(new Student() { Name = "王五", Subject = "数学", Scores = 43 }); studentList.Add(new Student() { Name = "王五", Subject = "语文", Scores = 57 }); studentList.Add(new Student() { Name = "王五", Subject = "英语", Scores = 78 });
3、基础查询
// Linq查询 var result = from item in studentList // 声明临时变量item,并说明其为对象列表studentList中的元素 where item.Scores >= 60 // 筛选成绩不小于60的对象 select item; // 将查询结果(item)添加到result中 // 遍历Linq查询结果,实际上是“Linq查询”是伴随foreach执行的,即在foreach执行前“Linq查询”不会执行 foreach (var model in result) //延迟执行 { Console.Write(model.Scores+" "); //输出87 78 }
4、查询排序
var result = from item in studentList where item.Scores >= 40 orderby item.Subject,item.Scores descending //先按照科目排序,再按照成绩倒序排序 select item; foreach (var model in result) { Console.Write(model.Subject + model.Scores+" "); //输出数学45 数学43 英语87 英语78 语文56 语文56 语文43 }
5、类型转换
var result = from item in studentList where item.Scores >= 40 orderby item.Subject,item.Scores descending select new { item.Subject, item.Scores }; //将student对象转换成一个新的匿名对象 foreach (var model in result) { Console.Write(model.Subject + model.Scores + " "); //输出数学45 数学43 英语87 英语78 语文56 语文56 语文43 }
6、拓展方法
var result = from item in studentList select item.Scores; Console.Write(result.Count());//获取列表的数量,输出9 Console.Write(result.Max());//获取最大的值,输出87 Console.Write(result.Min());//获取最小的值,输出28 Console.Write(result.Average());//获取平均值,输出51.55 Console.Write(result.Sum());//获取和,输出464 Console.Write(result.First());//获取第一个值,输出28 Console.Write(result.Last());//获取最后一个值,输出78 foreach (var v in result.Distinct()) { Console.Write(v+" "); }//获取去重后的值,输出 28 43 87 45 56 78
7、分组
var result = from itemList in studentList group itemList by itemList.Name;//根据名称作为键值,itemList为根据键值生成的对象集合 var queryResult = from item in result//二次加工,此时result.ToList().Count()等于3,为什么要toList呢,再次强调,result只是一段查询语句 let nameScores = Name + item.Sum(v => v.Scores) //使用let定义临时变量 let Space = " "//使用let定义临时变量 select nameScores + Space; foreach (var model in queryResult) { Console.Write(model);//输出 张三158 李四129 王五177 }
8、join
linq中的join都是inner join,可以通过代码实现left join 和 right join 的效果,这里我们举一下inner join和left join 的栗子。首先,我们需要引入一个新的数据源,如下所示:
private class Suggestions { public string StudentName { get; set; } public string Suggestion { get; set; } public string CreatedBy { get; set; } } var suggestionList = new List<Suggestions>(); suggestionList.Add(new Suggestions { StudentName="张三",Suggestion="数学语文有待提高",CreatedBy="张老师"}); suggestionList.Add(new Suggestions { StudentName="李四",Suggestion="所有科目有待提高",CreatedBy="李老师"}); //suggestionList.Add(new Suggestions { StudentName="王五",Suggestion="语文加油争取及格",CreatedBy="张老师"});
然后我们再通过linq,将studentList包装一下
var groupResult = from itemList in studentList group itemList by itemList.Name into tempGroup //获取分组数据 select new { Name = tempGroup.Key, Scores = tempGroup.Sum(v => v.Scores) };
inner join
var result = from stu in groupResult join sug in suggestionList on stu.Name equals sug.StudentName //关联评价列表, select new { stu.Name, stu.Scores, sug.Suggestion, sug.CreatedBy }; foreach (var model in result) { Console.Write(model.Name + model.Scores + model.Suggestion + model.CreatedBy); //输出 张三158数学语文有待提高张老师 李四129所有科目有待提高李老师 }
left join
var leftResult = from stu in groupResult join sug in suggestionList on stu.Name equals sug.StudentName into tempResult from tempSug in tempResult.DefaultIfEmpty()//关联评价列表, select new { stu.Name, stu.Scores, tempSug?.Suggestion, tempSug?.CreatedBy }; foreach (var model in leftResult) { Console.Write(model.Name + model.Scores + model.Suggestion + model.CreatedBy); //输出 张三158数学语文有待提高张老师 李四129所有科目有待提高李老师 王五177 }
以上是关于linq的简单使用的主要内容,如果未能解决你的问题,请参考以下文章