0

I have the following Xelemnt that contains a linq query within it. 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. 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?

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"))));
0

1 Answer 1

1

You might want to have a look at the DefaultIfEmpty method of Enumerable class. 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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.