C#Linq创建XDoc复制分组信息

C#Linq创建XDoc复制分组信息,c#,linq,C#,Linq,我试图通过组合两个查询来创建XDocument。其中一个查询已分组。我遇到的问题是,分组信息在集合中重复和迭代 组查询: XDocument testing = XDocument.Load(testXMLPath); var xlWarranty = (from warranty in testing.Descendants("Warranty") select new {

我试图通过组合两个查询来创建XDocument。其中一个查询已分组。我遇到的问题是,分组信息在集合中重复和迭代

组查询:

XDocument testing = XDocument.Load(testXMLPath);
var xlWarranty = (from warranty in testing.Descendants("Warranty")
                    select new
                    {
                        Service = (string)warranty.Element("ServiceLevelDescription").Value,
                        Provider = (string)warranty.Element("ServiceProvider").Value,
                        StartDate = (string)warranty.Element("StartDate").Value,
                        EndDate = (string)warranty.Element("EndDate").Value,
                        TypeOfWarranty = (string)warranty.Element("EntitlementType").Value
                    }).ToList();
标准查询:

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();
XDoc的创建:

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)
                )))))));
结果样本

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

OPTI 3020,TIGRISSFFFBTX
584290163
1CZTF02
2014-03-21T00:00:00
直拨电话服务
保留硬盘服务
下一工作日支持
白银特优支持
**戴尔劳工支持
4小时现场服务
POWEREDGE R720XD,奥卡S PE
339791846
1VF0TW1
2013-03-20T00:00:00
直拨电话服务
保留硬盘服务
下一工作日支持
白银特优支持
戴尔劳工支持
4小时现场服务

第二个资产节点不应包含这些担保。它不是在集合中迭代。

添加了创建内容的示例第二个资产不应该包含任何担保?第一项资产应该包含所有担保吗?应该包含,但信息不应该相同。源头不是导致它的原因。它重复放入保修信息中的内容,而不是转移到集合中的下一个迭代。因此,第一个资产应具有第一个保修,第二个资产应具有第二个保修,依此类推?第一个资产将具有一组保修。第二项资产将有一系列担保,依此类推。该信息来自“var XLV保修”