[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to fix this LINQ-XML expression

Posted on 2012-09-04
4
Medium Priority
?
503 Views
Last Modified: 2012-09-09
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
Comment
Question by:dominicwong
  • 3
4 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1500 total points
ID: 38366691
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
 

Author Comment

by:dominicwong
ID: 38366787
Sorry, it doesn't compile.
'There is no implicit conversion between 'string' and 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>'
0
 

Accepted Solution

by:
dominicwong earned 0 total points
ID: 38366803
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
 

Author Closing Comment

by:dominicwong
ID: 38380612
Thanks for your help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question