1
    <Itinerary Id="34" Code="2010STAN" Modified="2010-09-07 16:58:35">
<Itinerary Id="34" Code="2010STAN" Modified="2010-11-12 15:53:13">
<Itinerary Id="34" Code="2010STAN" Modified="2011-11-12 15:53:13">

I get that XML. and requirement is:

I want to get newer date / Large Date from Linq to XML query. for instance i want to get last node."2011-11-12 15:53:13"

PLEASE HELP!

1 Answer 1

2

Are you looking for the whole element, or just the date itself? For the date, it's easy:

var maxDate = list.Select(x => (DateTime) x.Attribute("Modified"))
                      .Max();

To get the element containing the latest date, you'd either have to sort them and take the first element, e.g.

var maxElement = list.OrderByDescending(x => (DateTime) x.Attribute("Modified"))
                     .First();

or use something like MaxBy from MoreLINQ:

var maxElement = list.MaxBy(x => (DateTime) x.Attribute("Modified"));
Sign up to request clarification or add additional context in comments.

2 Comments

At least one object must implement IComparable. My Code is : ' var t = (from resp_tourCodes in xmlDoc.Descendants("Itinerary") select resp_tourCodes).OrderByDescending(x => x.Attribute("Modified")).First();'
@Muhammad: Well yes, you haven't used the code I gave you... you need to cast the attribute to a DateTime, as per my answer. Note that your query expression is pretty pointless - you can just use var t = xmlDoc.Descendants("Itinerary").OrderByDescending(x => (DateTime) x.Attribute("Modified")).First();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.