代码改变世界

经典linq模板语句

2011-06-22 17:09  熬夜的虫子  阅读(230)  评论(0编辑  收藏  举报

1.简单形式:

var q = from c in db.Customers where c.City == "London" select c;  使用where筛选在伦敦的客户

var q = from e in db.Employees where e.HireDate >= new DateTime(1994, 1, 1) select e;   筛选1994 年或之后雇用的雇员:

2.关系条件形式:

 var q =
    from p in db.Products
    where p.UnitsInStock <= p.ReorderLevel && !p.Discontinued
    select p;   筛选库存量在订货点水平之下但未断货的产品

var q =
    from p in db.Products
    where p.UnitPrice > 10m || p.Discontinued
    select p;筛选出UnitPrice 大于10 或已停产的产品:
var q =
    db.Products.Where(p=>p.UnitPrice > 10m).Where(p=>p.Discontinued);
调用两次where以筛选出UnitPrice大于10且已停产的产品。
3.Frist形式
Shipper shipper = db.Shippers.First();
选择表中的第一个发货方。
Customer cust = db.Customers.First(c => c.CustomerID == "BONAP");
选择CustomerID 为“BONAP”的单个客户
Order ord = db.Orders.First(o => o.Freight > 10.00M);
选择运费大于 10.00 的订单

Select/Distinct操作符

1.简单用法:

var q =
    from c in db.Customers
    select c.ContactName;

这个示例返回仅含客户联系人姓名的序列。

 

2.匿名类型形式:

 

var q =
    from c in db.Customers
    select new {c.ContactName, c.Phone};

上面语句描述:使用 SELECT 和匿名类型返回仅含客户联系人姓名和电话号码的序列

var q =
    from e in db.Employees
    select new
    {
        Name = e.FirstName + " " + e.LastName,
        Phone = e.HomePhone
    };

上面语句描述:使用SELECT和匿名类型返回仅含雇员姓名和电话号码的序列,并将FirstName和LastName字段合并为一个字段“Name”,此外在所得的序列中将HomePhone字段重命名为Phone。

 

var q =

from p in db.Products select new { p.ProductID, HalfPrice = p.UnitPrice / 2 };

上面语句描述:使用SELECT和匿名类型返回所有产品的ID以及HalfPrice(设置为产品单价除以2所得的值)的序列。

 

 

3.条件形式:

说明:生成SQL语句为:case when condition then else。

var q =
    from p in db.Products
    select new
    {
        p.ProductName,
        Availability =
        p.UnitsInStock - p.UnitsOnOrder < 0 ? 
        "Out Of Stock" : "In Stock"
    };

上面语句描述:使用SELECT和条件语句返回产品名称和产品供货状态的序列。 

 

4.指定类型形式:

说明:该形式返回你自定义类型的对象集。
var q =
    from e in db.Employees
    select new Name
    {
        FirstName = e.FirstName,
        LastName = e.LastName
    };
上面语句描述:使用SELECT和已知类型返回雇员姓名的序列。

5.筛选形式:

说明:结合where使用,起到过滤作用。

var q =
    from c in db.Customers
    where c.City == "London"
    select c.ContactName;

上面语句描述:使用SELECT和WHERE返回仅含伦敦客户联系人姓名的序列。

 

6.shaped形式(整形类型):

说明:其select操作使用了匿名对象,而这个匿名对象中,其属性也是个匿名对象。

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 和匿名类型返回有关客户的数据的整形子集。查询顾客的ID和公司信息(公司名称,城市,国家)以及联系信息(联系人和职位)。

7.嵌套类型形式:

说明:返回的对象集中的每个对象DiscountedProducts属性中,又包含一个集合。也就是每个对象也是一个集合类。

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
    };

语句描述:使用嵌套查询返回所有订单及其OrderID 的序列、打折订单中项目的子序列以及免送货所省下的金额。

 

 

8.本地方法调用形式(LocalMethodCall):

这个例子在查询中调用本地方法PhoneNumberConverter将电话号码转换为国际格式。

var q = from c in db.Customers
         where c.Country == "UK" || c.Country == "USA"
         select new
         {
             c.CustomerID,
             c.CompanyName,
             Phone = c.Phone,
             InternationalPhone = 
             PhoneNumberConverter(c.Country, c.Phone)
         };

PhoneNumberConverter方法如下:

public string PhoneNumberConverter(string Country, string Phone)
{
    Phone = Phone.Replace(" ", "").Replace(")", ")-");
    switch (Country)
    {
        case "USA":
            return "1-" + Phone;
        case "UK":
            return "44-" + Phone;
        default:
            return Phone;
    }
}

下面也是使用了这个方法将电话号码转换为国际格式并创建XDocument

XDocument doc = new XDocument(
    new XElement("Customers", from c in db.Customers
              where c.Country == "UK" || c.Country == "USA"
              select (new XElement("Customer",
                      new XAttribute("CustomerID", c.CustomerID),
                      new XAttribute("CompanyName", c.CompanyName),
                      new XAttribute("InterationalPhone", 
                       PhoneNumberConverter(c.Country, c.Phone))
                     ))));

9.Distinct形式:

说明:筛选字段中不相同的值。用于查询不重复的结果集。生成SQL语句为:SELECT DISTINCT [City] FROM [Customers]

var q = (
    from c in db.Customers
    select c.City )
    .Distinct();

语句描述:查询顾客覆盖的国家。