首页 / 网页编程 / ASP.NET / LINQ体验(8)—LINQ to SQL语句之Union All/Union/Intersect和Top/Bottom和Pag
LINQ体验(8)—LINQ to SQL语句之Union All/Union/Intersect和Top/Bottom和Pag2010-11-19 cnblogs lyjLINQ体验(8)—LINQ to SQL语句之Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Intersect操作和Top/Bottom操作和Paging操作和SqlMethods操作 。Union All/Union/Intersect操作适用场景:对两个集合的处理,例如追加、合并、取相同项、相交项等等。Concat(连接)说明:连接不同的集合,不会自动过滤相同项;延迟。1.简单形式: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
);语句描述:返回所有消费者和雇员的电话和传真。2.复合形式:var q = (
from c in db.Customers
select new
{
Name = c.CompanyName,
c.Phone
}
).Concat(
from e in db.Employees
select new
{
Name = e.FirstName + " " + e.LastName,
Phone = e.HomePhone
}
);语句描述:返回所有消费者和雇员的姓名和电话。Union(合并)说明:连接不同的集合,自动过滤相同项;延迟。即是将两个集合进行合并操作,过滤相同的项。var q = (
from c in db.Customers
select c.Country
).Union(
from e in db.Employees
select e.Country
);语句描述:查询顾客和职员所在的国家。