Linq to XML 获取 id 集合中带有 id 的项目

发布于 2024-12-17 06:18:30 字数 2870 浏览 1 评论 0原文

鉴于下面的 XML,我将如何获取在 coordinators.coordinator > 中拥有职位的员工列表?位置 ID?

<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();

Given the XML below how would I go about getting a list of employees who have a position in the 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>

Here is what I have so far:

//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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2024-12-24 06:18:30

你可以尝试这样的事情:

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;

You can try something like that:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文