public void linq()
{
using (var con = new MyDbContext())
{
//簡單的linq語法
var q1 = from a in con.peoples where a.age > 10 orderby a.age descending select a;sql
//簡單的函數計算(count,min,max,sum)
var q2 = (from a in con.peoples select a).Sum(d => d.age);函數
//多列求和
var q3 = from a in con.peoples
group a by new
{
name = a.name
} into b
select new
{
age = b.Sum(t => t.age),
id = b.Sum(t => t.id),
//ids = b.Select(t => t.age > 10)
};排序
//排序order by desc/asc
var q4 = from a in con.peoples where a.age > 10 orderby a.age descending select a;ip
//top(1)
var q5 = (from a in con.peoples where a.age > 10 select a).FirstOrDefault(); select
//分頁
var q6 = (from a in con.peoples select a).Skip(2).Take(10);分頁
//相似like '%%'
var q7 = from a in con.peoples where a.name.Contains("武") select a;語法
//分組group by
var q8 = from a in con.peoples
group a by a.name into a
select new
{
a.Key,
age = a.Sum(c => c.age),
id = a.Max(c => c.id)
};查詢
//鏈接查詢
var q9 = from a in con.peoples
join b in con.peoples on a.id equals b.id
orderby a.id descending
select new { a = a, b = b };top
//sql中的In
var q10 = from a in con.peoples where (new int?[] { 1, 2, 3 }).Contains(a.id) select a;
}
}di