簡單的linq語法

2021-08-08 03:39:43 字數 4725 閱讀 4886

1.簡單的linq語法

//1var ss = from r in db.am_recproscheme

select r;

//2var ss1 = db.am_recproscheme;

//3string sssql = "select * from am_recproscheme";

2.帶where的查詢

//1var ss = from r in db.am_recproscheme

where r.rpid > 10

select r;

//2var ss1 = db.am_recproscheme.where(p => p.rpid > 10);

//3string sssql = "select * from am_recproscheme where rpid>10";

3.簡單的函式計算(count,min,max,sum)

//1獲取最大的rpid

//var ss = (from r in db.am_recproscheme

//          select r).max(p => p.rpid);

獲取最小的rpid

//var ss = (from r in db.am_recproscheme

//          select r).min(p => p.rpid);

//獲取結果集的總數

//var ss = (from r in db.am_recproscheme                  

//         select r).count();

//獲取rpid的和

var ss = (from r in db.am_recproscheme

select r).sum(p => p.rpid);

//2//var ss1 = db.am_recproscheme.max(p=>p.rpid);

//var ss1 = db.am_recproscheme.min(p => p.rpid);

//var ss1 = db.am_recproscheme.count() ;

var ss1 = db.am_recproscheme.sum(p => p.rpid);

response.write(ss);

//3string sssql = "select max(rpid) from am_recproscheme";

sssql = "select min(rpid) from am_recproscheme";

sssql = "select count(1) from am_recproscheme";

sssql = "select sum(rpid) from am_recproscheme";

4.排序order by desc/asc

var ss = from r in db.am_recproscheme

where r.rpid > 10

orderby r.rpid descending  //倒序

//  orderby r.rpid ascending   //正序

select r;

//正序

var ss1 = db.am_recproscheme.orderby(p => p.rpid).where(p => p.rpid > 10).tolist();

//倒序

var ss2 = db.am_recproscheme.orderbydescending(p => p.rpid).where(p => p.rpid > 10).tolist();

string sssql = "select * from am_recproscheme where rpid>10 order by rpid [desc|asc]";

5.top(1)

//如果取最後乙個可以按倒敘排列再取值

var ss = (from r in db.am_recproscheme                     

select r).firstordefault();

//()linq to ef 好像不支援 last() 

var ss1 = db.am_recproscheme.firstordefault();

//var ss1 = db.am_recproscheme.first();          

string sssql = "select top(1) * from am_recproscheme";

6.跳過前面多少條資料取餘下的資料

//1var ss = (from r in db.am_recproscheme

orderby r.rpid descending

select r).skip(10); //跳過前10條資料,取10條之後的所有資料   

//2  

var ss1 = db.am_recproscheme.orderbydescending(p => p.rpid).skip(10).tolist();

//3string sssql = "select * from  (select row_number()over(order by rpid desc) as rownum, * from [am_recproscheme]) as t where rownum>10";

7.分頁資料查詢

//1var ss = (from r in db.am_recproscheme

where r.rpid > 10

orderby r.rpid descending

select r).skip(10).take(10); //取第11條到第20條資料                   

//2 take(10): 資料從開始獲取,獲取指定數量(10)的連續資料

var ss1 = db.am_recproscheme.orderbydescending(p => p.rpid).where(p => p.rpid > 10).skip(10).take(10).tolist();

//3string sssql = "select * from  (select row_number()over(order by rpid desc) as rownum, * from [am_recproscheme]) as t where rownum>10 and rownum<=20";

8.包含,類似like '%%'

//1var ss = from r in db.am_recproscheme

where r.sortstext.contains("張")

select r;

//2var ss1 = db.am_recproscheme.where(p => p.sortstext.contains("張")).tolist();

//3string sssql = "select * from am_recproscheme where sortstext like '%張%'";

9.分組group by

//1var ss = from r in db.am_recproscheme

orderby r.rpid descending

group r by r.rectype into n

select new

;foreach (var t in ss)

//2var ss1 = from r in db.am_recproscheme

orderby r.rpid descending

group r by r.rectype into n

select n;

foreach (var t in ss1)

//3var ss2 = db.am_recproscheme.groupby(p => p.rectype);

foreach (var t in ss2)

//4string sssql = "select rectype,min(rpid),max(rpid),sum(rpid) from am_recproscheme group by rectype";

10.連線查詢 

//1var ss = from r in db.am_recproscheme

join w in db.am_test_result on r.rpid equals w.rsid

orderby r.rpid descending

select r;

//2var ss1 = db.am_recproscheme.join(db.am_test_result, p => p.rpid, r => r.rsid, (p, r) => p).orderbydescending(p => p.rpid).tolist();

//3string sssql = "select r.* from  [am_recproscheme] as r inner join [dbo].[am_test_result] as t on r.[rpid] = t.[rsid] order by r.[rpid] desc";

11.sql中的in

//1var ss = from p in db.am_recproscheme

where (new int? ).contains(p.rpid)

select p;

foreach (var p in ss)

//2string st = "select * from am_recproscheme where rpid in(24,25,26)";

Linq語法詳細

讀這篇文章之前,我先說下,每一種搜尋結果集,我都以三種方式變現出來。段1 linq語句查詢 段2 linq函式委託 段3 sql語句查詢。1 var ss from r in db.am recproscheme select r 2var ss1 db.am recproscheme 3strin...

Linq語法例項

linq語句即語言整合查詢 linq是一組語言特性和api,使得你可以使用統一的方式編寫各種查詢。用於儲存和檢索來自不同資料來源的資料,從而消除了程式語言和資料庫之間的不匹配,以及為不同型別的資料來源提供單個查詢介面。linq總是使用物件,因此你可以使用相同的查詢語法來查詢和轉換xml 物件集合 s...

LINQ語法入門

所有的linq表示式都必須以乙個指定資料來源的from子句開頭,以乙個表示要獲取資料的select子句 或者乙個定義了查詢結果分組的group子句 結尾的。from子句指定查詢操作的資料來源和範圍變數,其中資料來源不但包括查詢本身的資料來源,而且還包括子查詢的資料來源。範圍變數一般用來表示源序列中的...