D
Deckarep
Hey everyone,
Is there a more elegant or cleaner way of accomplishing the following
null check?
List<string> myString = null; //Purposely null list of strings to
show the example
XElement element = new XElement("Strings",
(myString != null) ?
(from string s in myString
where s.StartsWith("S")
select s) : null
);
This XElement yields the following result: <Strings/> and empty
Element.
I like that I can write linq to xml in nested declarations like this.
What I don't like now...is doing the null
check with the tertiary starts making the code ugly and less
readable. I know how to handle null values
when some of the contents in the collection may be null but what if
your collection is null itself??
If there's a better more Linq integrated way of handling this that
would be great.
Thanks,
Is there a more elegant or cleaner way of accomplishing the following
null check?
List<string> myString = null; //Purposely null list of strings to
show the example
XElement element = new XElement("Strings",
(myString != null) ?
(from string s in myString
where s.StartsWith("S")
select s) : null
);
This XElement yields the following result: <Strings/> and empty
Element.
I like that I can write linq to xml in nested declarations like this.
What I don't like now...is doing the null
check with the tertiary starts making the code ugly and less
readable. I know how to handle null values
when some of the contents in the collection may be null but what if
your collection is null itself??
If there's a better more Linq integrated way of handling this that
would be great.
Thanks,