Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:08:41+00:00 2026-06-16T04:08:41+00:00

I have an XML file with the following structure: <Entities> <Request> <ID> A1 </ID>

  • 0

I have an XML file with the following structure:

<Entities>
    <Request> 
       <ID> A1 </ID> <!-- Can be multiple records with same values -->
       <Finance>
          <StartDate> Some Date </StartDate> <!-- Unique per ID -->
       </Finance>
    <Request>
    <Request> ... </Request>
</Entities>

There can be multiple requests with same ID but in such cases, the StartDate needs to be different per Request.

I need to take out the latest two dates per ID.

If this were an SQL table with ID and StartDate columns, I’d use the following query which is working fine:

SELECT ID, StartDate 
FROM ( SELECT ID, StartDate, RANK() 
          OVER (PARTITION BY ID ORDER BY StartDate DESC) rank
       FROM Request ) 
WHERE rank IN ('1','2')

But I have the data in XML format and the best I could come up with was ordering the data according to ID, StartDate. I still need to dug out latest two dates for every ID there is.

var cafrequests =

from request in xdoc.Descendants("Request")
orderby (int)request.Element("ID"), 
(DateTime)request.Element("Finance").Element("StartDate") ascending
select new
{
    ID = request.Element("ID"),
    StartDate = request.Element("Finance").Element("StartDate"),
};

Using Take(2) would only fetch me the top 2 lines of data, not top 2 per ID.

So can anybody tell me what is the equivalent of the above SQL statement in LINQ? I don’t want to parse and manage XML using loops and conditionals in C# and I’m quite new to LINQ (read about it yesterday and started using it) and I’m still going through the documentation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:08:43+00:00Added an answer on June 16, 2026 at 4:08 am

    This works, I tested it:

            XDocument doc = XDocument.Load(@"Data.xml");
    
            var result2 = doc.Element("Entities")
                .Elements("Request")
                .GroupBy(key => key.Element("ID").Value, el => DateTime.Parse(el.Element("Finance").Element("StartDate").Value))
                .Select(el =>
                    new
                    {
                        id = el.Key,
                        max2 = el.OrderByDescending(date => date).Take(2)
                    });
    
    • doc.Element("Entities") – gets element named Entity which is a root element of the document, another way of retrieving this element would be doc.Root
    • Elements("Request") – gets elements named Request that are children of Entity
    • GroupBy – is a method somewhat similar to GROUP BY in SQL, first parameter is element that is going to be used for grouping, it is a child of a Request element, second parameter is an element selector function that parses DateTime from StartDate element (which is a child of Finance element which is a child of Request element)
    • .Select(el => new...) – method that creates anonymous type that holds ID and 2 records that have highest dates for ID

    Here is a query that gets the same result, but written in an query syntax (example above is method syntax):

            var result = from el in doc.Root.Elements("Request")
                         group DateTime.Parse(el.Element("Finance").Element("StartDate").Value) by el.Element("ID").Value into grouped
                         select new { 
                             id = grouped.Key,
                             max2 = (from el in grouped
                                     orderby el descending
                                     select el)
                                     .Take(2)
                         };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml file with the following structure: <main_tag> <first> <tag1>val1</tag1> <conf> <tag2>val2</tag2>
I have a large xml file (approx. 10 MB) in following simple structure: <Errors>
I have an XML file in the following structure. <NewDataSet> <markers> <name>name text</name> <desc>desc
I have an XML file with the following structure: <Products> <Product name=MyProduct1> <Components> <Component
The XML file have the following structure <RootElement> <TestElement Count=10/> </RootElement> I need to
I have the following file structure (XML files 'index.xml' in nested folders): index.xml foo/index.xml
I have a sample htm file with following structure and it POSTs the xml
I'm trying to merge two file that have the same structure, and some data
I have xml file whose structure is defined with following xsd: <?xml version=1.0 encoding=utf-8?>
I have an XML file similar in structure to the following: <Parent> <Child>5</Child> <Child>3</Child>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.