简体   繁体   English

LinQ to Xml查询并显示空元素

[英]LinQ to Xml query and displaying empty elements

I have the following Xelemnt that contains a linq query within it. 我有以下Xelemnt,其中包含linq查询。 The query works fine but I want to be able to return an empty recruiter element if there are no recruiters within the recruiter list (List<recruiters>) for a particular person. 该查询工作正常,但如果招聘人员列表(List<recruiters>)中没有特定人员的招聘人员,我希望能够返回一个空的招聘人员元素。 Is there any easy way to do this without checking to see if a recruiter element exists for a specific person after the xml has been built and if not then adding it? 是否有任何简单的方法可以执行此操作,而无需检查是否在构建xml之后是否为特定人员存在招聘者元素,如果不存在,则添加它?

XElement Person =
                    new XElement("Person",
                    new XElement("title", ""),
                    new XElement("id",""),
                    new XElement("url", ""),
                     (from Recruiter r in recruiters
                        where r.id == p.id
                        select new XElement("Recruiter",
                        new XElement("recruitername", r.recruitername),
                        new XElement("recruiteremail", r.recruiteremail),
                        new XElement("recruiterphone"))));

You might want to have a look at the DefaultIfEmpty method of Enumerable class. 您可能想看看Enumerable类的DefaultIfEmpty方法。 msdn msdn

XElement defaultRecruiter = new XElement("Recruiter");
XElement Person =
                new XElement("Person",
                new XElement("title", ""),
                new XElement("id",""),
                new XElement("url", ""),
                 (from Recruiter r in recruiters
                    where r.id == p.id
                    select new XElement("Recruiter",
                    new XElement("recruitername", r.recruitername),
                    new XElement("recruiteremail", r.recruiteremail),
                    new XElement("recruiterphone"))).DefaultIfEmpty(defaultRecruiter));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM