本文主要是介绍[C#] Linq常用语法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<strong>Linq常用语法</strong>
Student[] student = { new Student{ID=1, Name="小黑",Age=3,Class=1},new Student{ID=2,Name="小布",Age=5,Class=1},new Student{ID=3,Name="朱迪",Age=20,Class=2},new Student{ID=4,Name="狐尼克",Age=21,Class=2}};
StudentClass[] studentClass = { new StudentClass{ID=1, Name="一班"},new StudentClass{ID=2, Name="二班"}};
1 简单的linq语法
//1 简单的linq语法var s11 = from r in studentselect r;var s12 = student;//"select * from student";
2.linq where 查询
var s21 = from r in studentwhere r.ID > 2select r;var s22 = student.Where(p => p.ID > 2);//"select * from student where ID>2";
3.linq 简单的函数计算(count,min,max,sum)
//获取最大的IDvar s311 = (from r in studentselect r).Max(p => p.ID);//获取最小的IDvar s312 = (from r in studentselect r).Min(p => p.ID);//获取结果集的总数var s313 = (from r in studentselect r).Count();//获取ID的和var s314 = (from r in studentselect r).Sum(p => p.ID);var s321 = student.Max(p => p.ID);var s322 = student.Min(p => p.ID);var s323 = student.Count();var s324 = student.Sum(p => p.ID);// "select max(ID) from student";// "select min(ID) from studente";// "select count(1) from student";// "select sum(ID) from student";
4.linq 排序order by desc/asc
var s41 = from r in studentwhere r.ID > 1orderby r.ID descending //倒序//orderby r.ID ascending //正序select r;//正序var s42 = student.OrderBy(p => p.ID).Where(p => p.ID > 1).ToList();//倒序var s43 = student.OrderByDescending(p => p.ID).Where(p => p.ID > 1).ToList();// "select * from student where ID>1 order by ID [desc|asc]";
5.linq top
//如果取最后一个可以按倒叙排列再取值var s51 = (from r in student select r).FirstOrDefault();//()linq to ef 好像不支持 Last() var s52 = student.FirstOrDefault();var s53 = student.First();// "select top(1) * from student";
6.linq 跳过前面多少条数据取余下的数据
var s61 = (from r in studentorderby r.ID descendingselect r).Skip(10); //跳过前10条数据,取10条之后的所有数据 var s62 = student.OrderByDescending(p => p.ID).Skip(10).ToList();
// "select * from (select ROW_NUMBER()over(order by ID desc) as rowNum, * from [student]) as t where rowNum>10";
7.linq 分页数据查询
var s71 = (from r in studentwhere r.ID > 10orderby r.ID descendingselect r).Skip(10).Take(10); //取第11条到第20条数据 //Take(10): 数据从开始获取,获取指定数量(10)的连续数据var s72 = student.OrderByDescending(p => p.ID).Where(p => p.ID > 10).Skip(10).Take(10).ToList();//"select * from (select ROW_NUMBER()over(order by ID desc) as rowNum, * from [student]) as t // where rowNum>10 and rowNum<=20";
8.linq 包含,类似like '%%'
var s81 = from r in studentwhere r.Name.Contains("小")select r;var s82 = student.Where(p => p.Name.Contains("小")).ToList();// "select * from student where Name like '%小%'";
9.linq 分组group by
var s91 = from r in studentorderby r.ID descendinggroup r by r.Class into nselect new{n.Key, //这个Key是recTyperpId = n.Sum(r => r.ID), //组内rpId之和MaxRpId = n.Max(r => r.ID),//组内最大rpIdMinRpId = n.Min(r => r.ID), //组内最小rpId};foreach (var t in s91){Console.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId);}//2var s92 = from r in studentorderby r.ID descendinggroup r by r.Class into nselect n;foreach (var t in s92){Console.Write(t.Key + "--" + t.Min(p => p.ID));}//3var s93 = student.GroupBy(p => p.Class);foreach (var t in s93){Console.Write(t.Key + "--" + t.Min(p => p.ID));}//"select Class,min(ID),max(ID),sum(ID) from student group by Class";
10.linq 连接查询
//1var s101 = from r in studentjoin w in studentClass on r.Class equals w.IDorderby r.ID descendingselect r;//2
var s102 = student.Join(studentClass, p => p.Class, r => r.ID, (p, r) => p).OrderByDescending(p => p.ID).ToList();//3// "select r.* from student as r // inner join [dbo].[studentClass] as t // on r.[Class] = t.[ID] //order by r.[ID] desc";
11.linq 中的In
var s111 = from p in studentwhere (new int?[] { 2,3,4 }).Contains(p.ID)select p;foreach (var p in s111){Console.Write(p.Name);}//2//"select * from student where ID in(2,3,4)";
参考原文:http://www.cnblogs.com/knowledgesea/p/3897665.html
这篇关于[C#] Linq常用语法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!