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>
Here are the code snippets for your both requirements:
Select Last Status where ContractID is 2
Select Top N Statuses where ContractID is 2
For your new requirement, you can use this code snippet as a replacement for your SQL query: