代码改变世界

關於Linq框架SQL語句的寫法一(select/Distinct操作符) <续>

2009-11-04 10:41  北冥有魚,其名為坤、  阅读(334)  评论(0编辑  收藏  举报
6.Shaped形式(整形类型)、
  说明:其select操作使用了匿名对象,而这个匿名对象中,其属性也是个匿名对象、
var q = from c in db.Customers select new
{
    c.CustomerID,
    ComplayInfo 
= new {c.CompanyName,c.City,c,County},
    contactInfo 
= new {c.ContactName,c.ContacTitle}
};
7.嵌套类型形式、
说明:返回的对象集中的每个对象DiscountedProducts属性中,又包含一个集合。也就是每个对象也是一个集合类。
      示例:返回所有订单及OrderID的序列,打折订单中项目的子序列已经免费送货所省下的经费.

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

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方法如下:
Code
下面也是使用了这个方法将电话号码转换为国际格式并创建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();

 

好了,查询在这里就告一段落,本文由本人经多方查找收集并结合实际开发经验所著,大致可分为这几种,不过还有别的表现形式可以得到相同结果,
比如where可以点出来啊.很多都可以点出来的.不过只是一个"妈妈"而已啦。