我试图通过组合两个查询来创建XDocument。其中一个查询已分组。我遇到的问题是分组信息是复制和迭代集合。
群组查询:
var xlBaseInfo = (from asset in testing.Descendants("DellAsset")
select new
{
Product = (string)asset.Element("MachineDescription").Value,
OrderNumber = (string)asset.Element("OrderNumber").Value,
ServiceTag = (string)asset.Element("ServiceTag").Value,
ShipDate = (string)asset.Element("ShipDate").Value
}).ToList();
标准查询:
var groupByWarrany = xlWarranty.GroupBy(x => x.Service);
var newDocument = new XDocument(new XElement("Machine", xlBaseInfo.Select(z =>
new XElement("Asset",
new XElement("Product", z.Product),
new XElement("OrderNumber", z.OrderNumber),
new XElement("ServiceTag", z.ServiceTag),
new XElement("ShipDate", z.ShipDate),
//Duplicating information and not moving on to next starting here --
new XElement("Warranties", groupByWarrany.Select(x =>
new XElement("Warranty",
new XElement("ServiceDescription", x.Key)
)))))));
创建XDoc:
<Machine>
<Asset>
<Product>OPTI 3020,TIGRISSFFFBTX</Product>
<OrderNumber>584290163</OrderNumber>
<ServiceTag>1CZTF02</ServiceTag>
<ShipDate>2014-03-21T00:00:00</ShipDate>
<Warranties>
<Warranty>
<ServiceDescription>DirectLine Service</ServiceDescription>
</Warranty>
<Warranty>
<ServiceDescription>Keep Your Hard Drive Service</ServiceDescription>
</Warranty>
<Warranty>
<ServiceDescription>Next Business Day Support</ServiceDescription>
</Warranty>
<Warranty>
<ServiceDescription>Silver Premium Support</ServiceDescription>
</Warranty>
<Warranty>
**<ServiceDescription>Dell Labor Support</ServiceDescription>
</Warranty>
<Warranty>
<ServiceDescription>4 Hour On-Site Service</ServiceDescription>
</Warranty>
</Warranties>
</Asset>
<Asset>
<Product>POWEREDGE R720XD, ORCA S PE</Product>
<OrderNumber>339791846</OrderNumber>
<ServiceTag>1VF0TW1</ServiceTag>
<ShipDate>2013-03-20T00:00:00</ShipDate>
<Warranties>
<Warranty>
<ServiceDescription>DirectLine Service</ServiceDescription>
</Warranty>
<Warranty>
<ServiceDescription>Keep Your Hard Drive Service</ServiceDescription>
</Warranty>
<Warranty>
<ServiceDescription>Next Business Day Support</ServiceDescription>
</Warranty>
<Warranty>
<ServiceDescription>Silver Premium Support</ServiceDescription>
</Warranty>
<Warranty>
<ServiceDescription>Dell Labor Support</ServiceDescription>
</Warranty>
<Warranty>
<ServiceDescription>4 Hour On-Site Service</ServiceDescription>
</Warranty>
</Warranties>
结果示例:
{{1}}
第二个资产节点不应包含这些保证。它没有遍历集合。