Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XDocument _document = XDocument.Parse(XMLString);
- <Line>
- <ITEMNMBR>18-4695</ITEMNMBR>
- <UNITCOST>2.00000</UNITCOST>
- <UNITPRCE>7.00000</UNITPRCE>
- <QUANTITY>15.00000</QUANTITY>
- </Line>
- <Line>
- <ITEMNMBR>18-4695</ITEMNMBR>
- <UNITCOST>3.00000</UNITCOST>
- <UNITPRCE>7.00000</UNITPRCE>
- <QUANTITY>22.00000</QUANTITY>
- </Line>
- var lines = _document.Descendants("Line")
- .Select(l => l.Elements().ToDictionary(e => e.Name.LocalName, e => e.Value)).ToList();
- var itemNumbers = lines.Where(dict => dict.ContainsKey("ITEMNMBR"))
- .Select(dict => dict["ITEMNMBR"])
- .Distinct()
- .ToList();
- foreach (string itemNumber in itemNumbers)
- {...
- var qty = lines.Where(dict => dict.ContainsValue(itemNumber))
- .Select(dict => dict["QUANTITY"])
- .ToList();
- int sum = qty.Sum(x => Convert.ToDecimal(x.Value));
- var qty = lines.Where(dict => dict.ContainsValue(itemNumber))
- .Select(dict => dict["QUANTITY"]) // here you are selecting the value
- .ToList();
- int sum = qty.Sum(x => Convert.ToDecimal(x));
- var sum = lines.Where(dict => dict.ContainsValue(itemNumber))
- .Sum(dict => Convert.ToDecimal(dict["QUANTITY"]));
- decimal sum = qty.Sum(x => !string.IsNullOrEmpty(x) ? Convert.ToDecimal(x) : 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement