鉴于下面的XML,我将如何获得在coordinators.coordinator中担任职务的员工名单> position_ids?
<employee id="000001">
<username>Bernard</username>
<first_name>BERNARD</first_name>
<last_name>FISHER</last_name>
<business_phone>011111111111</business_phone>
<cell_phone>011111111112</cell_phone>
<hr_contact_name>PETER MANNING</hr_contact_name>
<contract_description>Permanent</contract_description>
<positions>
<position id="00000002" isPrimary="1">
<title>DEVELOPMENT MANAGER</title>
<department id="DV001">DEVELOPMENT</department>
<manager_position>00000002</manager_position>
<coordinators>
<coordinator position_id="00013662"/>
<coordinator position_id="00014488"/>
<coordinator position_id="00022675"/>
<coordinator position_id="00024364"/>
</coordinators>
</position>
</positions>
</employee>
<employee id="000002">
<!-- ... --->
</employee>
这是我到目前为止所做的:
//GET EMPLOYEE WITH CORORDINATORS AND SUBORINATES WITH POSITION ID
var q = from c in d.Elements().Elements().Elements("positions").Elements("position") where (string)(c.Attribute("id")) == "028782"
select new
{
PositionID = c.Parent.Parent.Attribute("id")
,Username = c.Parent.Parent.Element("username").Value
,FirstName = c.Parent.Parent.Element("first_name").Value
,LastName = c.Parent.Parent.Element("last_name").Value
,ContractDecription = c.Parent.Parent.Element("contract_description").Value
,Title = c.Element("title").Value
,Coordinators = (from coordinator
in d.Elements().Elements().Elements("positions").Elements("position")
join p in c on coordinator.Attribute("id").Value equals p.Attribute("id").Value
select new
{
PositionID = coordinator.Attribute("id")
,Username = coordinator.Parent.Parent.Element("username").Value
,FirstName = coordinator.Parent.Parent.Element("first_name").Value
,LastName = coordinator.Parent.Parent.Element("last_name").Value
,ContractDecription = coordinator.Parent.Parent.Element("contract_description").Value
,thenode = coordinator
})
,thenode = c
};
q.Dump();
答案 0 :(得分:3)
你可以尝试这样的事情:
var coordinatorIds =
d.Elements("employee")
.Elements("positions")
.Elements("coordinators")
.Elements("coordinator")
.Select(c => c.Attribute("position_id").Value)
.Distinct()
.ToArray();
var coordinators =
from c in d.Elements("employee")
join id in coordinatorIds on c.Attribute("id").Value equals id
select c;