转载:C#3.0入门系列(五)[原dlinq入门系列]
http://www.microsoft.com/downloads/details.aspx?FamilyID=1FF0B35D-0C4A-40B4-915A-5331E11C39E6&displaylang=en
大家快去下载呀,我也好和大家一起体验该版本最新功能呀。
dlinq也更名为linq to sql.本文也跟着做相应变化,稍候,我会去更新前面的文章。我们先接着讲linq的语法。
Select操作
最简单的
1, var q =
from c in db.Customers
select c.ContactName;
匿名类的from c in db.Customers
select c.ContactName;
1, var q =
from c in db.Customers
select new {c.ContactName, c.Phone};
2, var q =
from e in db.Employees
select new {Name = e.FirstName + " " + e.LastName, Phone = e.HomePhone};
3, var q =
from p in db.Products
select new {p.ProductID, HalfPrice = p.UnitPrice / 2};
条件的from c in db.Customers
select new {c.ContactName, c.Phone};
2, var q =
from e in db.Employees
select new {Name = e.FirstName + " " + e.LastName, Phone = e.HomePhone};
3, var q =
from p in db.Products
select new {p.ProductID, HalfPrice = p.UnitPrice / 2};
var q =
from p in db.Products
select new {p.ProductName, Availability = p.UnitsInStock - p.UnitsOnOrder < 0 ? "Out Of Stock": "In Stock"};
这种条件的会被翻译成sql中{case when condition then else}的。from p in db.Products
select new {p.ProductName, Availability = p.UnitsInStock - p.UnitsOnOrder < 0 ? "Out Of Stock": "In Stock"};
name type形式的:
var q =
from e in db.Employees
select new Name {FirstName = e.FirstName, LastName = e.LastName};
只所以是name type的,是因为Name类是已经定义好的,也就是说,你可以用这种方式,返回你需要类型的对象集.from e in db.Employees
select new Name {FirstName = e.FirstName, LastName = e.LastName};
shaped形式的:
var q =
from c in db.Customers
select new {
c.CustomerID,
CompanyInfo = new {c.CompanyName, c.City, c.Country},
ContactInfo = new {c.ContactName, c.ContactTitle}
};
该形式,其select操作使用了匿名对象,而这个匿名对象中,其属性也是个匿名对象。from c in db.Customers
select new {
c.CustomerID,
CompanyInfo = new {c.CompanyName, c.City, c.Country},
ContactInfo = new {c.ContactName, c.ContactTitle}
};
nested形式的:
var q =
from o in db.Orders
select new {
o.OrderID,
DiscountedProducts =
from od in o.OrderDetails
where od.Discount > 0.0
select od,
FreeShippingDiscount = o.Freight
};
其返回的对象集中的每个对象DiscountedProducts属性中,又包含一个小的集合。也就是每个对象也是一个集合类。from o in db.Orders
select new {
o.OrderID,
DiscountedProducts =
from od in o.OrderDetails
where od.Discount > 0.0
select od,
FreeShippingDiscount = o.Freight
};
Distinct形式的:
var q = (
from c in db.Customers
select c.City )
.Distinct();
该形式,筛选该字段中不相同的值。会被翻译为from c in db.Customers
select c.City )
.Distinct();
select distinct city from customers
where操作:
最简单的
1, var q =
from c in db.Customers
where c.City == "London"
select c;
2, var q =
from e in db.Employees
where e.HireDate >= new DateTime(1994, 1, 1)
select e;
或与关系的where条件
from c in db.Customers
where c.City == "London"
select c;
2, var q =
from e in db.Employees
where e.HireDate >= new DateTime(1994, 1, 1)
select e;
1, var q =
from p in db.Products
where p.UnitsInStock <= p.ReorderLevel && !p.Discontinued
select p;
2, var q =
from p in db.Products
where p.UnitPrice > 10m || p.Discontinued
select p;
3, var q =
db.Products.Where(p=>p.UnitPrice > 10m).Where(p=>p.Discontinued);
在上例中,1和2语句先被翻译成类似3语句的形式,再被翻译成sql语句,送回数据服务器。他们基本上一样的。from p in db.Products
where p.UnitsInStock <= p.ReorderLevel && !p.Discontinued
select p;
2, var q =
from p in db.Products
where p.UnitPrice > 10m || p.Discontinued
select p;
3, var q =
db.Products.Where(p=>p.UnitPrice > 10m).Where(p=>p.Discontinued);
欠套在first操作中的where条件:
first操作,其实质就是在sql语句前,加了一个top 1.
1, Customer cust = db.Customers.First(c => c.CustomerID == "BONAP");
2 Order ord = db.Orders.First(o => o.Freight > 10.00M);
第一个例子,是筛选customerid为"BONAP"的客户,第二个筛选订单运费大于10的订单。First操作必须用这种级连的形式。比如
2 Order ord = db.Orders.First(o => o.Freight > 10.00M);
Shipper shipper = db.Shippers.First();
也可以把linq的expression和级连的形式混合使用,比如第一个例子,加入first操作,
var q =
(from c in db.Customers
where c.City == "London"
select c).First();
如果加入first操作,其返回是一个具体的对象,而不是一个集合。如果first操作没有条件,它只是简单的在sql语句中添加top 1,如果有条件,它在翻译时,就会加入条件语句。
(from c in db.Customers
where c.City == "London"
select c).First();