linq to sql实战
1.Concat(连接)
连接不同集合的记录,不会过滤相同的记录。
(from c in Customers
select c.City).Concat
(
from c in Customers
select c.Phone
)
注意:City和Phone都是在一列当中
2.Union(合并)
连接不同集合的记录,会过滤相同的记录。
(from c in Customers
select c.City).Concat
(
from c in Customers
select c.Phone
)
3.Intersect(相交)
连接不同集合的记录,取相同的记录。
4.Except
连接不同集合的记录,排除相交项。是指第一个集合里面排除与第二个集合里相同的记录。
Sql当中的top操作和bottom操作
1. take
取前几条记录
2. skip
说明:跳过集合的前n个元素
SqlMethods操作
在LINQ to SQL语句中,为我们提供了SqlMethods操作,进一步为我们提供了方便,例如Like方法用于自定义通配表达式,Equals用于相比较是否相等。
Like
from c in Customers
where SqlMethods.Like(c.Country,"%c%")
select c.Country
DateDiffDay
说明:在两个变量之间比较。分别有:DateDiffDay、DateDiffHour、DateDiffMillisecond、DateDiffMinute、DateDiffMonth、DateDiffSecond、DateDiffYear
from c in Orders
where SqlMethods.DateDiffDay(c.OrderDate,c.ShippedDate)<10
select c
Compiled Query操作
说明:在之前我们没有好的方法对写出的SQL语句进行编辑重新查询,现在我们可以这样做,看下面一个例子:
//1.创建compiled query
NorthwindDataContext db = new NorthwindDataContext();
var fn = CompiledQuery.Compile(
(NorthwindDataContext db2, string city) =>
from c in db2.Customers
where c.City == city
select c);
//2.查询城市为London的消费者,用LonCusts集合表示,这时可以用数据控件绑定
var LonCusts = fn(db, "
//3.查询城市为Seattle的消费者
var SeaCusts = fn(db, "
Distinct
var q = (
from c in db.Customers
select c.City )
.Distinct();
Where |
过滤;延迟 |
Select |
选择;延迟 |
Distinct |
查询不重复的结果集;延迟 |
Count |
返回集合中的元素个数,返回INT类型;不延迟 |
LongCount |
返回集合中的元素个数,返回LONG类型;不延迟 |
Sum |
返回集合中数值类型元素之和,集合应为INT类型集合;不延迟 |
Min |
返回集合中元素的最小值;不延迟 |
Max |
返回集合中元素的最大值;不延迟 |
Average |
返回集合中的数值类型元素的平均值。集合应为数字类型集合,其返回值类型为double;不延迟 |
Aggregate |
根据输入的表达式获取聚合值;不延迟 |
Join操作
Join操作分三种:
1. Join
from c in Orders
join o in Customers on c.CustomerID equals o.CustomerID
select new
{
c.CustomerID,
o.ContactName
}
2. SelectMany(这是一对多,多对多)
一对多
Customers与Orders是一对多关系。即Orders在Customers类中以EntitySet形式出现
from c in Customers
from o in c.Orders
select new
{
c.CustomerID,
o.OrderID
}
多对多
from c in Customers
from o in c.Orders
select new
{
c.CustomerID,
o.OrderID
}
用多对多的情况是:
一般有三个表,它们的关系是1:M:1
也就是说中间那个表有外键,连接第一个和第三表中
自关联关系:
var q =
from e1 in db.Employees
from e2 in e1.Employees
where e1.City == e2.City
select new {
FirstName1 = e1.FirstName, LastName1 = e1.LastName,
FirstName2 = e2.FirstName, LastName2 = e2.LastName,
e1.City
};
3. group join
var q =
from c in db.Customers
join o in db.Orders on c.CustomerID
equals o.CustomerID into orders
select new
{
c.ContactName,
OrderCount = orders.Count()
};
let语句是重命名。let位于第一个from和select语句之间。
var q =
from c in db.Customers
join o in db.Orders on c.CustomerID
equals o.CustomerID into ords
let z = c.City + c.Country
from o in ords
select new
{
c.ContactName,
o.OrderID,
z
};
Composite Key(组合键)
var q =
from o in db.Orders
from p in db.Products
join d in db.OrderDetails
on new
{
o.OrderID,
p.ProductID
} equals
new
{
d.OrderID,
d.ProductID
}
into details
from d in details
select new
{
o.OrderID,
p.ProductID,
d.UnitPrice
};
说明:使用三个表,并且用匿名类来表示它们之间的关系。它们之间的关系不能用一个键描述清楚,所以用匿名类,来表示组合键。还有一种是两个表之间是用组合键表示关系的,不需要使用匿名类。
Order by
var q =
from o in db.Orders
where o.ShipCity == "
orderby o.Freight
select o;
posted on 2008-08-15 23:33 Love Fendi 阅读(367) 评论(0) 编辑 收藏 举报