<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 student
select r;
var s12 = student;
//"select * from student";
2.linq where 查詢
var s21 = from r in student
where r.ID > 2
select r;
var s22 = student.Where(p => p.ID > 2);
//"select * from student where ID>2";
3.linq 簡單的函數計算(count,min,max,sum)
//獲取最大的ID
var s311 = (from r in student
select r).Max(p => p.ID);
//獲取最小的ID
var s312 = (from r in student
select r).Min(p => p.ID);
//獲取結果集的總數
var s313 = (from r in student
select r).Count();
//獲取ID的和
var s314 = (from r in student
select 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 student
where r.ID > 1
orderby 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 student
orderby r.ID descending
select 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 student
where r.ID > 10
orderby r.ID descending
select 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 student
where 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 student
orderby r.ID descending
group r by r.Class into n
select new
{
n.Key, //這個Key是recType
rpId = n.Sum(r => r.ID), //組內rpId之和
MaxRpId = n.Max(r => r.ID),//組內最大rpId
MinRpId = n.Min(r => r.ID), //組內最小rpId
};
foreach (var t in s91)
{
Console.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId);
}
//2
var s92 = from r in student
orderby r.ID descending
group r by r.Class into n
select n;
foreach (var t in s92)
{
Console.Write(t.Key + "--" + t.Min(p => p.ID));
}
//3
var 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 連接查詢
//1
var s101 = from r in student
join w in studentClass on r.Class equals w.ID
orderby r.ID descending
select 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 student
where (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