linq在查詢時非常方便,下面分享一下我在專案開發常用的幾個關鍵字。為了演示,新建了3個類(student,course,score),分別是學生資訊表,課程資訊表,成績資訊表。
/// /// 學生資訊表
///
class student
//學生主鍵
public string stunumber //學號
public string stuname //姓名
public int stuage //年齡
}/// /// 課程資訊表
///
class course
//課程主鍵
public string couname //課程名稱
}/// /// 成績資訊表
///
class score
//成績主鍵
public int stuid //學生主鍵
public int couid //課程主鍵
public int scomark //分數
}
linq常用語法:
static void main(string args)
,new student(),
new student()
};ilistcourselist = new list()
};ilistscorelist = new list() ,
new score() ,
};#endregion
#region where ,select
var query1 = from stu in studentlist
where stu.stuage == 19
select new //返回匿名物件
;console.writeline("---------------------------------query1------------------------------");
foreach (var q in query1)
,姓名:", q.stunumber, q.stuname));
}#endregion
#region join
var query2 = from sco in scorelist
join stu in studentlist
on sco.stuid equals stu.stuid
join cou in courselist
on sco.couid equals cou.couid
select new
;console.writeline("---------------------------------query2------------------------------");
foreach (var q in query2)
,課程:,分數:", q.stuname, q.couname, q.scomark));
}#endregion
#region case
var query3 = from sco in scorelist
join stu in studentlist
on sco.stuid equals stu.stuid
join cou in courselist
on sco.couid equals cou.couid
select new
;console.writeline("---------------------------------query3------------------------------");
foreach (var q in query3)
,課程:,分數:", q.stuname, q.couname, q.scomark));
}#endregion
#region left join
var query4 = from stu in studentlist
join sco in scorelist
on stu.stuid equals sco.stuid into temp
from tt in temp.defaultifempty()
select new
;console.writeline("---------------------------------query4------------------------------");
foreach (var q in query4)
,分數:", q.stuname, q.scomark));
}#endregion
#region let
var query5 = from sco in scorelist
join stu in studentlist
on sco.stuid equals stu.stuid
join cou in courselist
on sco.couid equals cou.couid
let passmark = "[" + sco.scomark + "]" + (sco.scomark >= 60 ? "及格" : "不及格")
select new
;console.writeline("---------------------------------query5------------------------------");
foreach (var q in query5)
,課程:,分數:", q.stuname, q.couname, q.passmark));
}#endregion
#region order by
var query6 = from stu in studentlist
//orderby stu.stuage ascending //公升序
orderby stu.stuage descending //降序
select new //返回匿名物件
;console.writeline("---------------------------------query6------------------------------");
foreach (var q in query6)
,姓名:,年齡:", q.stunumber, q.stuname, q.stuage));
}#endregion
#region distinct
var query7 = (from stu in studentlist
orderby stu.stuage descending
select new //返回匿名物件
).distinct();
console.writeline("---------------------------------query7------------------------------");
foreach (var q in query7)
", q.stuage));
}#endregion
#region top 1
var query8 = (from stu in studentlist
where stu.stuage == 19
select new //返回匿名物件
).firstordefault();
console.writeline("---------------------------------query8------------------------------");
if (query8 != null)
,姓名:,年齡:",
query8.stunumber, query8.stuname, query8.stuage));
}#endregion
console.readkey();
}
輸出結果:
Linq專題之var關鍵字
在c 1.0,c 2.0中宣告乙個變數時,總是要指定變數的型別,如果不指定變數型別編譯器就會報錯,產生編譯錯誤。在c 3.0中我們可以不指定變數的具體型別,而使用乙個新的關鍵字 var 來指定變數的型別,該變數的實際型別將在其初始化表示式中推到出來。下面例項 中,我們用var宣告了3個區域性變數,並...
python基礎之常用關鍵字總結
到python3.6為止,python內建的關鍵字有33個,比python2.7的版本多了2個。下面總結一下python3的關鍵字的使用。直譯器在載入上下文的時候,如果遇到一些預先設定的變數值,就會觸發直譯器內建的一些操作,這些預定的變數值就是關鍵字。檢視當前版本的關鍵字資訊,使用 help key...
python基礎之常用關鍵字總結
到python3.6為止,python內建的關鍵字有33個,比python2.7的版本多了2個。下面總結一下python3的關鍵字的使用。直譯器在載入上下文的時候,如果遇到一些預先設定的變數值,就會觸發直譯器內建的一些操作,這些預定的變數值就是關鍵字。檢視當前版本的關鍵字資訊,使用 help key...