您的位置:首页 > 数据库

【转载】C# LINQ to SQL

2013-01-11 22:03 495 查看
1、Concat(连接不同的集合不会自动过滤相同项。会延迟计算)

varq=(fromcindb.Customers
selectc.Phone
).Concat(
fromeindb.Employees
selecte.HomePhone);
varq=(fromcindb.Customers
selectnew
{
Name=c.CustomerName,
Phone=c.Phone
}).Concat(
fromeindb.Employees
selectnew
{
Name=e.EmployeeName,
Phone=e.HomePhone
});


2、Union(合并,自动过滤相同的项。会延迟计算)

varq=(fromcindb.Customers
selectc.Country).Union(
fromeindb.Employees
selecte.Country);


3、Intersect(交。会延迟计算)

varq=(fromcindb.Customers
selectc.Country).Intersect(
fromeindb.Employees
selecte.Country);

4、Except(差,A-B。从A集合排除A交B。会延迟计算)

varq=(fromcindb.Customers
selectc.Country).Except(
fromeindb.Employees
selecte.Country);


5、Top、Bottom(取出指定数量的数据。会延迟计算)

6、Take(获取集合的前n个数据。会延迟计算)

varq=(fromeindb.Employees
orderbye.HireDate
selcte).Take(5);


7、Skip(跳过集合的前n个数据。会延迟计算)

varq=(frompindb.Products
orderbyp.UnitPricedescending
selectp).Skip(10);

选择10种最贵的产品之外的所有产品

8、TakeWhile(直到某一条件不成立才停止获取。会延迟计算)
即用其条件去依次判断源序列中的元素,返回符合判断条件的元素,该判断操作将在返回false或源序列的末尾结束

9、SkipWhile(顾名思义,同上)

10、Paging(分页操作)

varq=(fromcindb.Customers
orderbyc.CustomerName
selectc).Skip(50).Take(10);


11、Like

varq=fromcindb.Customers
    whereSqlMethods.Like(c.CustomerID,"C%")
    selectc;


查询消费者ID没有“AXOXT”形式的消费者:

varq=fromcindb.Customers
    where!SqlMethods.Like(c.CustomerID,"A_O_T")
    selectc;DateDiffDay


在两个时间变量之间比较。分别有:DateDiffDay、DateDiffHour、DateDiffMillisecond、DateDiffMinute、DateDiffMonth、DateDiffSecond、DateDiffYear:

varq=fromoindb.Orders
    whereSqlMethod.DateDiffDay(o.OrderDate,o.ShippedDate)<10
    selecto;


查询在创建订单后10天内发货的所有订单

12、CompiledQuery(预编译查询)

NorthwindDataContextdb=newNorthwindDataContext();
varfn=CompiledQuery.Compile(
(NorthwindDataContextdb2,stringcity)=>
  fromcindb2.Customers
  wherec.City==city
  selectc);
varlondonCusts=fn(db,"London");
varseaCusts=fn(db,"Seattle");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: