如何使用 C# 从以下其中 ContractID 为 2 的 xml 文件中选择最后 1 个状态?
<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>
以及如何使用 linq to xml 或任何其他方式选择 top n
什么是相当于以下 sql 查询的 linq to xml 代码:
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
如果存在名为 Contracts.xml、SysType.xml 和 EmployeesActions.xml 的 xml 文件,如下 Contracts.xml
<Contracts>
<Contract>
<ID>1</ID>
<ContractNo>Mob124444</ContractNo>
</Contract>
<Contract>
<ID>2</ID>
<ContractNo>Mob124445</ContractNo>
</Contract>
</Contracts>
雇员行动.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>
系统类型.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>