• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
works guo

成功的渴望与生俱来---- 成功是获得赞美与尊重的最有效的途径
博客园    首页    新随笔    联系   管理    订阅  订阅

很酷的let clause的应用

这里是LINQ to XML利用let暂时存放子节点的数据,再从查询let中的数据得到XML中子节点多个属性.
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2509542&SiteID=1&mode=1
这里是LINQ to XML利用let暂时存放子节点的数据,再从查询let中的数据得到XML中子节点多个属性.

<cars>
  
<car name="Toyota Coupe">
    
<profile name="Vendor" value="Toyota"/>
    
<profile name="Model" value="Celica"/>
    
<profile name="Doors" value="2"/>
    
<support name="Racing" value="yes"/>
    
<support name="Towing" value="no"/>
  
</car>
  
<car name="Honda Accord Aerodec">
    
<profile name="Vendor" value="Honda"/>
    
<profile name="Model" value="Accord"/>
    
<profile name="Doors" value="4"/>
    
<support name="Racing" value="no"/>
    
<support name="Towing" value="yes"/>
  
</car>
</cars>


from car in root.Elements("car")
let profiles 
=
  from profile 
in car.Elements("profile")
  select 
new {
    Name 
= profile.Attribute("name").Value,
    Value 
= profile.Attribute("value").Value
  }

let supports 
=
  from support 
in car.Elements("support")
  select 
new {
    Name 
= support.Attribute("name").Value,
    Value 
= support.Attribute("value").Value
  }

select 
new Car {
  Name 
= car.Attribute("name").Value,
  Vendor 
= profiles.Single(prof => prof.Name == "Vendor").Value,
  Model 
= profiles.Single(prof => prof.Name == "Model").Value,
  Doors 
= int.Parse(profiles.Single(prof => prof.Name == "Doors").Value),
  RacingSupport 
= supports.Single(sup => sup.Name == "Racing").Value == "yes"
}
;


其实更精彩的是在一个老外的blog上的一个超级查询表达方式.

http://blogs.msdn.com/lukeh/archive/2007/10/01/taking-linq-to-objects-to-extremes-a-fully-linqified-raytracer.aspx


posted @ 2008-04-15 19:23  works guo  阅读(296)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3