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

  • SEARCH
  • Home
  • 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 8828319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:34:44+00:00 2026-06-14T07:34:44+00:00

How to select last 1 Status from the following xml file where ContractID is

  • 0

How to select last 1 Status from the following xml file where ContractID is 2 using C#?

<actions>
     <Action>
    <ID>2</ID>
    <ContractID>1</ContractID>
    <EmployeeID>1</EmployeeID>
    <Date>2012-09-04 00:00:00.000</Date>
    <Reply/>
    <Status>4002</Status>
</Action>
    <Action>
    <ID>2</ID>
    <ContractID>2</ContractID>
    <EmployeeID>1</EmployeeID>
    <Date>2012-09-04 00:00:00.000</Date>
    <Reply/>
    <Status>4005</Status>
</Action>
    <Action>
    <ID>2</ID>
    <ContractID>2</ContractID>
    <EmployeeID>1</EmployeeID>
    <Date>2012-09-04 00:00:00.000</Date>
    <Reply/>
    <Status>4008</Status>
</Action>
</actions>

and how to select top n too using linq to xml or any other way


and what is the linq to xml code that is equivalent to the following sql query:

Select contracts.ID, contracts.ContractNo, 
    (select FieldName from tbl_SysType where ID =
    (select top 1 status from tbl_EmployeesActions where ContractID=contracts.ID and Status is not null order by ID desc ))as ContractStatus
from tbl_Contracts as contracts

if there are xml files named Contracts.xml, SysType.xml, and EmployeesActions.xml as below
Contracts.xml

<Contracts>
     <Contract>
    <ID>1</ID>
    <ContractNo>Mob124444</ContractNo>      
</Contract>
    <Contract>
    <ID>2</ID>
    <ContractNo>Mob124445</ContractNo>      
</Contract>
</Contracts>

EmployeesActions.xml

    <actions>
     <Action>
    <ID>2</ID>
    <ContractID>1</ContractID>
    <EmployeeID>1</EmployeeID>
    <Date>2012-09-04 00:00:00.000</Date>
    <Reply/>
    <Status>4002</Status>
</Action>
    <Action>
    <ID>2</ID>
    <ContractID>2</ContractID>
    <EmployeeID>1</EmployeeID>
    <Date>2012-09-04 00:00:00.000</Date>
    <Reply/>
    <Status>4005</Status>
</Action>
    <Action>
    <ID>2</ID>
    <ContractID>2</ContractID>
    <EmployeeID>1</EmployeeID>
    <Date>2012-09-04 00:00:00.000</Date>
    <Reply/>
    <Status>4008</Status>
</Action>
</actions>

Systype.xml

<SysTypes>
    <systype>
        <ID>4002</ID>
        <FieldName>New</FieldName>
    </systype>
<systype>
        <ID>4005</ID>
        <FieldName>Opened</FieldName>
    </systype>
<systype>
        <ID>4008</ID>
        <FieldName>Closed</FieldName>
    </systype>
</SysTypes>
  • 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-14T07:34:45+00:00Added an answer on June 14, 2026 at 7:34 am

    Here are the code snippets for your both requirements:

    XDocument doc = XDocument.Load(@"XMLFile.xml");
    
    var statuses = doc.Descendants("Action")
                      .Where(s => s.Element("ContractID").Value.Equals("2"))
                      .Select(s => s.Element("Status").Value);
    

    Select Last Status where ContractID is 2

    var lastStatus = statuses.Last();
    

    Select Top N Statuses where ContractID is 2

    int N = 2; // assuming TOP 2
    var topNStatuses = statuses.Take(N);
    

    For your new requirement, you can use this code snippet as a replacement for your SQL query:

    XDocument employees = XDocument.Load(@"EmployeesActions.xml");
    XDocument contracts = XDocument.Load(@"Contracts.xml");
    XDocument sysTypes = XDocument.Load(@"Systype.xml");
    
    var result = employees.Descendants("Action")
                .Join(sysTypes.Descendants("systype"),
                    e => e.Element("Status") != null ? e.Element("Status").Value : "",
                    s => s.Element("ID").Value,
                    (e, s) => new
                    {
                        ID = e.Element("ID").Value,
                        ContractID = e.Element("ContractID").Value,
                        Status = s.Element("FieldName").Value
                    })
                .Where(x => !String.IsNullOrEmpty(x.Status))
                .OrderByDescending(x => x.ID)
                .Take(1)
                .Join(contracts.Descendants("Contract"),
                    e => e.ContractID,
                    c => c.Element("ID").Value,
                    (e, c) => new
                    {
                        ContractID = c.Element("ID").Value,
                        ContractNo = c.Element("ContractNo").Value,
                        Status = e.Status
                    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to select the last record from a table in MySQL using
I would like to select the last visible <span class=delimiter'></span> element. The following syntax
How do I select the globally-last text node using xpath? I tried this, but
SELECT DISTINCT u.first, u.last FROM users u, user_friends f WHERE f.userId = 1 AND
How would I select all but the last child using CSS3 selectors? For example,
I'm trying convert xml from one format to other using the XslCompiledTransform in c#.
I'm using the following code to get some data from a table. $collection =
I've created the following SQL query to export order data from the database. SELECT
I have the following query: SELECT l.id, (SELECT amount FROM lead_status WHERE lead_id =
I know the following statement is invalid: SELECT Terms.LName, Terms.FName, AS400.SystemID, AS400.userid, AS400.Status, AS400.text

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.