Mar 4

Written by: Soul Solutions
Tuesday, 4 March 2008 

BronwenWeeGo.jpgWas playing with Linq to XML today and wanted to do an orderby based on one of the fields in the xml file.

It's a bit different to SQL as the orderby goes between the from and the select.

My code below shows selecting everything and ordering by the eventdate descending.

  
XDocument eventsXml = XDocument.Load(events.xml);
var temp = from feed in eventsXml.Descendants("LocalEvent")
           orderby Convert.ToDateTime(feed.Element("EventDate").Value) descending
           select new LocalEvent
           {
               ID = Convert.ToInt32(feed.Element("ID").Value),
               EventDate = Convert.ToDateTime(feed.Element("EventDate").Value),
               EventDescription = feed.Element("EventDescription").Value,
               EventName = feed.Element("EventName").Value,
               Latitude = Convert.ToDouble(feed.Element("Latitude").Value),
               Longitude = Convert.ToDouble(feed.Element("Longitude").Value),
               Location = feed.Element("Location").Value

           };
return temp.ToList();

kick it on DotNetKicks.com

Tags:

2 comment(s) so far...

Re: Order By with LINQ to XML

hi there man, does this definitely work?

I've been searching for an example using orderby date using LINQ to XML for ages.

Really hope this works get back to me. Thanks.

By Jhey on   Saturday, 14 February 2009

Re: Order By with LINQ to XML

http://msdn.microsoft.com/en-us/vbasic/bb738051.aspx

By Mahendran on   Friday, 14 August 2009

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
Copyright © 2002-2009 Soul Solutions Pty Ltd. | Login