• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

how to fix this LINQ-XML expression

Hi experts
I am new to LINQ-XML, I wonder how to fix this expression. Thanks in advance,

There is an exception since 'tgSetting.TgIDs' can be 'null'.
var e = from tgSetting in tgSettings
            select new XElement("Tg", new XAttribute("Name", tgSetting.TgListName),
                      new XElement("Tgrp",from tgID in tgSetting.TgIDs
                                                         where tgID != null
                                                         select new XElement("Tg", new XAttribute("Number", tgID.ToString()))));

What I would like to have is if it is 'null', then output the following:
<Tg Name="gp 0">
         <Tgrp>
         </Tgrp>
</Tg>
0
dominicwong
Asked:
dominicwong
  • 3
2 Solutions
 
käµfm³d 👽Commented:
Try:

var e = from tgSetting in tgSettings
        select new XElement("Tg", new XAttribute("Name", tgSetting.TgListName),
                                  new XElement("Tgrp", tgSetting.TgIDs == null ?
                                                            string.Empty :
                                                            from tgID in tgSetting.TgIDs
                                                            where tgID != null
                                                            select new XElement("Tg", new XAttribute("Number", tgID.ToString()))));

Open in new window

0
 
dominicwongAuthor Commented:
Sorry, it doesn't compile.
'There is no implicit conversion between 'string' and 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>'
0
 
dominicwongAuthor Commented:
Got it. Should put the checking outside. Thanks kaufmed

var e = from tgSetting in tgSettings
        select new XElement("Tg", new XAttribute("Name", tgSetting.TgListName),
                           (tgSetting.TgIDs != null)?
                                  new XElement("Tgrp", from tgID in tgSetting.TgIDs
                                                       where tgID != null
                                                       select new XElement("Tg", new XAttribute("Number", tgID.ToString())))
                                     :
                                  new XElement("Tgrp",string.Empty)  
                                                       );
0
 
dominicwongAuthor Commented:
Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now