> I am just learning linq and am stopped by trying to get a sum of element
> values.
[quoted text clipped - 17 lines]
>
> but this does not work and I don't know the correct format: qty.Sum
Here is one example to do that:
Dim order As XDocument = <?xml version="1.0"?>
<LineItems>
<lineItem>
<Itemnum>123</Itemnum>
<Quantity>6</Quantity>
</lineItem>
<lineItem>
<Quantity>5</Quantity>
</lineItem>
</LineItems>
Dim qty As IEnumerable(Of XElement) = From item In
order.Elements("LineItems").Descendants("lineItem") Select
item.<Quantity>(0)
Console.WriteLine(qty.Sum(Function(q) CType(q, Decimal)))
And here is another using the LINQ query syntax:
Dim order As XDocument = <?xml version="1.0"?>
<LineItems>
<lineItem>
<Itemnum>123</Itemnum>
<Quantity>6</Quantity>
</lineItem>
<lineItem>
<Quantity>5</Quantity>
</lineItem>
</LineItems>
Dim qty As IEnumerable(Of XElement) = From item In
order.Elements("LineItems").Descendants("lineItem") Select
item.<Quantity>(0)
Console.WriteLine(Aggregate q In qty Into Sum(CType(q, Decimal)))
Note that the Select has item.<Quantity>(0), that is crucial.

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Rick - 08 Jul 2008 17:52 GMT
Thanks Martin, that was a big time saver!
Rick
>> I am just learning linq and am stopped by trying to get a sum of element
>> values.
[quoted text clipped - 54 lines]
>
> Note that the Select has item.<Quantity>(0), that is crucial.