XElement属性group by

时间:2014-02-17 07:14:20

标签: c# grouping xelement

我有xml

<Customer>
  <Vendor Value="Vodafone">
    <CustomerBarcode>10010</CustomerBarcode>
    <Description>Vodafone CepLira 10 TL</Description>
    <UnitAmount>0</UnitAmount>
  </Vendor>
  <Vendor Value="Vodafone">
    <CustomerBarcode>10020</CustomerBarcode>
    <Description>Vodafone CepLira 20 TL</Description>
    <UnitAmount>0</UnitAmount>
  </Vendor>
  <Vendor Value="Vodafone">
    <CustomerBarcode>10030</CustomerBarcode>
    <Description>Vodafone CepLira 30 TL</Description>
    <UnitAmount>0</UnitAmount>
  </Vendor>
  <Vendor Value="Vodafone">
    <CustomerBarcode>10050</CustomerBarcode>
    <Description>Vodafone CepLira 50 TL</Description>
    <UnitAmount>0</UnitAmount>
  </Vendor>
  <Vendor Value="Vodafone">
    <CustomerBarcode>10100</CustomerBarcode>
    <Description>Vodafone CepLira 100 TL</Description>
    <UnitAmount>0</UnitAmount>
  </Vendor>
</Customer>
 var xml = new XElement("Customer",
                from prod in _customer
                group prod by new { prod.Vendor, prod.CustomerBarcode, prod.Description, prod.UnitAmount } into g
                select new XElement("Vendor",new XAttribute("Value",g.Key.Vendor),
                    new XElement("CustomerBarcode", g.Key.CustomerBarcode),
                     new XElement("Description", g.Key.Description),
                     new XElement("UnitAmount", g.Key.UnitAmount)
              ));

我想按供应商价值分组:

  <Customer>
  <Vendor Value="Vodafone">
    <CustomerBarcode>10010</CustomerBarcode>
    <Description>Vodafone CepLira 10 TL</Description>
    <UnitAmount>0</UnitAmount>
    <CustomerBarcode>10020</CustomerBarcode>
    <Description>Vodafone CepLira 20 TL</Description>
    <UnitAmount>0</UnitAmount>
     <CustomerBarcode>10020</CustomerBarcode>
    <Description>Vodafone CepLira 20 TL</Description>
    <UnitAmount>0</UnitAmount>
     <CustomerBarcode>10030</CustomerBarcode>
    <Description>Vodafone CepLira 30 TL</Description>
    <UnitAmount>0</UnitAmount>
     <CustomerBarcode>10050</CustomerBarcode>
    <Description>Vodafone CepLira 50 TL</Description>
    <UnitAmount>0</UnitAmount>
     <CustomerBarcode>10100</CustomerBarcode>
    <Description>Vodafone CepLira 100 TL</Description>
    <UnitAmount>0</UnitAmount> 
  </Vendor>
</Customer>

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您应该定义分组键:在您的情况下,它将是Vender属性值本身,而不是所有属性。

您应该使用SelectMany将所有群组项目合并为一个大集合。

以下应该做的诀窍:

var xml = new XElement("Customer",
                from prod in _customer
                group prod by prod.Vendor into g
                let vendor = g.Key
                let items = g.SelectMany(x => x)
                select
                    new XElement("Vendor",
                        new XAttribute("Value",g.Key.Vendor),
                        items.SelectMany(i => 
                            new XElement[] {
                                 new XElement("CustomerBarcode", i.CustomerBarcode),
                                 new XElement("Description", i.Description),
                                 new XElement("UnitAmount", i.UnitAmount)
                            }
                        )
                    )
                );

但是,只是为了说清楚:我认为XML结构并不是一个好的选择。你应该将单元组合在一起,可能使用类似的东西:

<Customer>
  <Vendor Value="Vodafone">
    <Unit>
      <CustomerBarcode>10010</CustomerBarcode>
      <Description>Vodafone CepLira 10 TL</Description>
      <UnitAmount>0</UnitAmount>
    </Unit>
    <Unit>
      <CustomerBarcode>10020</CustomerBarcode>
      <Description>Vodafone CepLira 20 TL</Description>
      <UnitAmount>0</UnitAmount>
    </Unit>
    <Unit>
      <CustomerBarcode>10020</CustomerBarcode>
      <Description>Vodafone CepLira 20 TL</Description>
      <UnitAmount>0</UnitAmount>
    </Unit>
    <Unit>
      <CustomerBarcode>10030</CustomerBarcode>
      <Description>Vodafone CepLira 30 TL</Description>
      <UnitAmount>0</UnitAmount>
    </Unit>
    <Unit>
      <CustomerBarcode>10050</CustomerBarcode>
      <Description>Vodafone CepLira 50 TL</Description>
      <UnitAmount>0</UnitAmount>
    </Unit>
    <Unit>
      <CustomerBarcode>10100</CustomerBarcode>
      <Description>Vodafone CepLira 100 TL</Description>
      <UnitAmount>0</UnitAmount>
    </Unit>
  </Vendor>
</Customer>

它使文档更具可读性。要实现这一点,请使用以下LINQ查询:

var xml = new XElement("Customer",
                from prod in _customer
                group prod by prod.Vendor into g
                let vendor = g.Key
                let items = g.SelectMany(x => x)
                select
                    new XElement("Vendor",
                        new XAttribute("Value",g.Key.Vendor),
                        items.Select(i => 
                            new XElement("Unit",
                                 new XElement("CustomerBarcode", i.CustomerBarcode),
                                 new XElement("Description", i.Description),
                                 new XElement("UnitAmount", i.UnitAmount)
                            )
                        )
                    )
                );