Link to home
Start Free TrialLog in
Avatar of dominicwong
dominicwong

asked on

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>
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dominicwong
dominicwong

ASKER

Sorry, it doesn't compile.
'There is no implicit conversion between 'string' and 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>'
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help.