Only get the value if a condition is true
- by Autolycus
I am checking to see if element1, element 2 or element 3 exists and then add them to finalData if they exist. However
if one of those dont exist or are not true then I just want to add the elements whose bool value is true! Below is my code
bool hasElement1 = (
from Playlist in loaded.Descendants("Node")
select Playlist.Descendants("Element1").Any()
).Single();
bool hasElement2 = (
from Playlist in loaded.Descendants("Node")
select Playlist.Descendants("Element2").Any()
).Single();
bool hasElement3 = (
from Playlist in loaded.Descendants("Node")
select Playlist.Descendants("Element2").Any()
).Single();
var finalData = from x in loaded.Descendants("Node")
select new
{
Element1 = x.Descendants("Element1").First().Value,
Element2 = x.Descendants("Element2").First().Value,
Element3 = x.Descendants("Element3").First().Value,
};