到目前为止,我有一个简单的LINQ查询,使用XML文件来构建字典或customerid并订购:
var Limit =来自xml.Descendants中的c(“Customer”) 选择新的 { customerID = c.Descendants(“customerid”)。First()。Value, order = c.Descendants(“order”)。First()。Value,
};
我需要返回不同的客户ID以及每个customerID的'订单总数。例如,我有:
CustomerID = 123订单= 200
CustomerID = 123订单= 100
CustomerID = 123订单= 50
CustomerID = 456订单= 100
CustomerID = 456订单= 100
我需要:
客户ID = 123 OrderTotal = 350
CustomerID = 456 OrderTotal = 200
我知道有很多类似的问题,但我对LINQ很新。
由于
答案 0 :(得分:0)
如果没有看到确切的XML输入结构就很难编写代码,但请尝试以下几行:
var query = from cust in xml.Descendants("Customer")
group cust by (int)cust.Element("customerid") into g
select new {
customerID = g.Key,
total = g.Elements("order").Sum(el => (decimal)el)
}
[编辑] 使用评论中提到的样本
var query = from cust in xml.Descendants("Customer")
group cust by (int)cust.Element("customerid") into g
select new {
customerID = g.Key,
total = g.Elements("ordertotal").Sum(el => (decimal)el)
}