有关 Lambda linq练习 有待整理
1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。(select sname,ssex,class from student)
Students.Select(s=> new { sname=>s.sname,ssex=>s.ssex, class=>s.class})
linq:from s in Students select new{s.sname, }
2.查詢教師所有的單位即不重復的Depart列。select distinct depart from teacher
teachers.distinct().Select(s=>s.depart)
linq: from s in teachers.Distinct() select s.depart
3、 查詢Student表的所有記錄。select * from Student
student.Select(s=>s)
linq: from s in student select s;
4、 查詢Score表中成績在60到80之間的所有記錄。 select * from score where grages bewteen 60 and 80
score.Where(s=>(s.grages>60 && s.grages<80) );
linq: from s in score where s.grages>60 && s.grages<80 select s;
5、 查詢Score表中成績?yōu)?5,86或88的記錄。select * from score where grages in (85,86,88)
linq: from s in Score Where ( new decimal[] {85,86,88}.Contains(s.grage))
Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.grage))
6、 查詢Student表中"95031"班或性別為"女"的同學記錄。select * from student where class ='95031' or ssex= '女'
student.Where(s=> (s.class=="95031" || s.sex="女"))
from s in student where s.class=="" || s.sex=="" select s;
?
7、 以Class降序查詢Student表的所有記錄。 select * from student order by Class DESC
student.OrderByDescing(s=> s.Class)
from s in student order by s.class descending select s;
?
8、 以Cno升序、Degree降序查詢Score表的所有記錄。select * from Score order by cno asc and grage desc
score.OrderByDescing(s=>s.grage).OrderBy(s=>s.cno);
from s in score order by s.grage descending order by s.cno ascending select s;
9、 查詢"95031"班的學生人數。select count(*) from Students where class='95031'
students.Select(s=>s.class=='95031').Count();
///students.Where(s=>s.class=='').Select(s=>s).Count();
10、查詢Score表中的最高分的學生學號和課程號。
11、查詢'3-105'號課程的平均分。select avg(grage) from score where cno='3-105'
score.where(s=>s.con=="3-105").Select(s=>s.grage).Avgage();
12、查詢Score表中至少有5名學生選修的并以3開頭的課程的平均分數。
select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5
linq: from s in score where s.con.StartWith("3") group s by s.Cno into cc where cc.count>5
select cc.Avgage(s=>s.grage)
13、查詢最低分大于70,最高分小于90的Sno列。select Sno from score group by Sno having min(grage)>70 and
max(grage)<90
linq: from s in score group s by s.sno into cc where cc.min(grage)>70 && cc.Max(grage)<90
select new {sno =>cc.key}
lambda:
scores.GroupBy(s=>s.sno).Where(ss=> ( (ss.Min(cc=>cc.grage)>70) && (ss.Max(cc=>cc.grage)<90 )))
.select(ss=> new {sno=>ss.key})
14、查詢所有學生的Sname、Cno和Degree列。
select s.sname,sc.cno,sc.degree from student s,score sc where s.sno = sc.sno
from s in Student join sc in score on s.sno=sc.sno
select new {s.sname,sc.cno, sc.degree}
lambda: Student.join(scores,s=>s.sname,
sc=>sc.cno,
(s,sc)=> new{ sname =s.sname, cno=sc.cno, degree=sc.degree })
//釋放資源 .Dispose();
------------------------------------------------------------
.skip(1).Take(4) Skip 上面這段語句的意思是從第二條數據開始顯示4條
AppendAndWhere //附加AND查詢條件 JoinOnAndWhere //joinon篩選條
Any(): 判斷集合中是否有元素滿足某一條件
僅返回沒有訂單的客戶:var q =from c in db.Customers where !c.Orders.Any() select c;
All(): 判斷集合中所有元素是否都滿足某一條件。
var q =from c in db.Customers where c.Orders.All(o => o.ShipCity == c.City) select c;
語句描述:這個例子返回所有訂單都運往其所在城市的客戶或未下訂單的客戶。
Contains() :用于判斷集合中是否包含有某一元素
Concat(連接): 連接不同的集合,不會自動過濾相同項
var q = (from c in db.Customers select c.Phone).Concat(from c in db.Customers select c.Fax).Concat(from e in db.Employees select e.HomePhone);
語句描述:返回所有消費者和雇員的電話和傳真。
Union ::連接不同的集合,自動過濾相同項;延遲。即是將兩個集合進行合并操作,過濾相同的項。
Intersect(相交) :取相交項;延遲。即是獲取不同集合的相同項(交集)。
Except (與非) :排除相交項;
.Implicit(隱式) Explicit(顯式)
ToArray:將序列轉換為數組
ToList:將序列轉換為泛型列表
-------------------------------------------------------------
轉載于:https://www.cnblogs.com/wangzhe688/p/7561203.html
總結
以上是生活随笔為你收集整理的有关 Lambda linq练习 有待整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联动菜单实现思路
- 下一篇: python基础之名称空间和作用域、函数