Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / July 2008

Tip: Looking for answers? Try searching our database.

linq to xml Sum

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick - 08 Jul 2008 15:38 GMT
I am just learning linq and am stopped by trying to get a sum of element
values.

The xml is like this:

<LineItems>
   <lineItem>
       <Itemnum>123</Itemnum>
       <Quantity>6</Quantity>
   </lineItem>
   <lineItem>
       ...
   </lineItem>
</LineItems>

I use this Linq:

           Dim qty As IEnumerable(Of XElement) = From item In
Order.Elements("LineItems").Descendants("lineItem") Select item.<Quantity>

but this does not work and I don't know the correct format: qty.Sum

Can someone suggest how to do this or a tutorial?

Rick
Martin Honnen - 08 Jul 2008 17:08 GMT
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.