Linq to SQL学习笔记——SELECT/WHERE语句
1.SELECT语句
(1) 普通查询
var list = from s in db.student select s.stuname;
(2) 匿名类查询
var list = from c in db.customer select new {name = c.firstname+" "+c.lastname};
(3) 条件查询
var list = from l in db.grade select new {name = l.name; level = l.score>60?"pass":"failed"};
(4) 指定类型查询
var list = from e in db.card select new Name {first = e.firstname;last=e.lastname};
(5) 筛选查询
var list = from s in db.student where s.id < 10 select s.name;
(6) 匿名对象嵌套查询
var list = from e in db.data select new {
company = new { companyname = e.name;
companyphone = e.phone},
manager = e.manager;
}
(7) 调用方法查询
var list = from s in db.student select new {name=s.name,age=ageconvert(s.age)};
(8) Distinct形式
var list = (from s in db.student select s.name).distinct();
2. WHERE语句
(1) 普通条件查询
var list = from s in db.student where s.name = "Jack" select s;
(2) 关系条件查询
var list = from s in db.student where s.age>20 && s.sex ==1;
var list = db.student.where(s=>s.age>20).where(s=>s.sex==1)
(3) First形式(相当于SQL中Top(1))
student stu = db.student.first(s=>s.age>20);
本文出自 “咖啡色的梦” 博客
相关新闻>>
- 发表评论
-
- 最新评论 更多>>