我有 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>
我该怎么做?
您应该定义分组键:在您的情况下,它将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)
)
)
)
);