很酷的let clause的应用
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2509542&SiteID=1&mode=1
这里是LINQ to XML利用let暂时存放子节点的数据,再从查询let中的数据得到XML中子节点多个属性.
其实更精彩的是在一个老外的blog上的一个超级查询表达方式.
http://blogs.msdn.com/lukeh/archive/2007/10/01/taking-linq-to-objects-to-extremes-a-fully-linqified-raytracer.aspx
这里是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>
<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"
};
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