Reputation: 1241
I am reading data from XML file using from..select linq query, the data is huge. the biggest set of data I can read is 1100 lines, each line consists of 100s of characters. This causes my phone to hang and application crashes. On Emulator, it works fine,although it takes considerable amount of time to load.
Now what I want, is based on some conditions I want include the data elements in the query. As anexample, what I have now is...
var dataSet = from r in something.Elements("chapter")
select new dictChapter{
Name = r.Attribute("Name").Value,
Desc1 = r.Attribute("Description1").Value,
Desc2 = r.Attribute("Description2").Value,
Desc3 = r.Attribute("Description3").Value
};
ListBox.DataContext = dataSet;
But I want to select the Descriptions based on the settings. I want something like (I know it doesn't work, but I want explain what I want to do)
var dataSet = from r in something.Elements("chapter")
select new dictChapter{
Name = r.Attribute("Name").Value,
if (ShowDesc1 == true)
Desc1 = r.Attribute("Description1").Value,
if (ShowDesc2 == true)
Desc2 = r.Attribute("Description2").Value,
if (ShowDesc3 == true)
Desc3 = r.Attribute("Description3").Value
};
ListBox.DataContext = dataSet;
Thanks very much
Upvotes: 2
Views: 871
Reputation: 125620
You can try something like:
var dataSet = from r in something.Elements("chapter")
select new dictChapter{
Name = r.Attribute("Name").Value,
Desc1 = (ShowDesc1 ? r.Attribute("Description1").Value : null),
Desc2 = (ShowDesc2 ? r.Attribute("Description2").Value : null),
Desc3 = (ShowDesc3 ? r.Attribute("Description3").Value : null),
};
It is not exactly what you want, because all Desc1-3 properties are always set on every element, but they are set to null if according ShowDesc1-3 is false.
Upvotes: 2
Reputation: 15663
Use the conditional operator "?:"
var dataSet = from r in something.Elements("chapter")
select new dictChapter{
Name = r.Attribute("Name").Value,
Desc1 = ShowDesc1 ? r.Attribute("Description1").Value : String.Empty,
Desc2 = ShowDesc2 ? r.Attribute("Description2").Value : String.Empty,
Desc3 = ShowDesc3 ? r.Attribute("Description3").Value : String.Empty,
};
Upvotes: 2