【发布时间】:2010-05-26 22:07:57
【问题描述】:
我有一个 IEnumerable
var zones = inventory.Select(c => new {
ZoneID = c.ZoneId
, ZoneName = c.ZoneName
, Direction = c.Direction
}).Distinct();
不,我想根据区域和地点创建 xml。 ***place 是“someClass”的属性。
var xml = new XElement("MSG_StationInventoryList"
, new XElement("StationInventory"
, zones.Select(station =>
new XElement("station-id", station.ZoneID)
, new XElement("station-name", station.ZoneName))));
当我尝试添加“站名”元素时,这不会编译,因为“站”超出了范围。但是,我在“ZoneId”之后删除了括号,站在范围内并且我检索了站名。唯一的问题是该元素是“station-id”的后代。这不是所需的输出。他们应该是兄弟姐妹。我究竟做错了什么?最后在“站名”元素之后,我需要另一个复杂类型,即集合。称之为“地点”。它将有称为“地点”的子元素。它的数据将来自 IEnumerable,我只想要当前区域具有“ZoneId”的“地点”。谁能指出我正确的方向?从原始 IEnumerable 中选择不同的区域是错误的吗?这个对象包含了我需要的所有数据。我只需要使其具有层次结构。感谢所有指针。
干杯,
克里斯在圣地亚哥
****编辑
var zones = inventory.Select(c => new {
ZoneID = c.ZoneId
, ZoneName = c.ZoneName
, Direction = c.Direction
}).Distinct();
var xml = new XElement("MSG_StationInventoryList"
, zones.Select(station => new XElement("StationInventory"
, new XElement("station-id", station.ZoneID)
, new XElement("station-name", station.ZoneName)
, new XElement("station-travel-direction", station.Direction)
, new XElement("detector-list"
, inventory.Where(p=>p.ZoneId == station.ZoneID).Select(plaza=>
new XElement("detector"
, new XElement("detector-id", plaza.PlazaId)))))));
这最终奏效了。
【问题讨论】:
-
我不确定我是否了解这些地方......你能澄清一下你的意思吗?可以举个例子吗?
标签: c# linq lambda linq-to-xml