Sample code:
public string[] GetMeetingPersonnel(DateTime dateMeeting, string strHistoryDatabase)
{
DateTime dateMonday = dateMeeting.StartOfWeek(DayOfWeek.Monday);
XDocument docAssignHistory = XDocument.Load(strHistoryDatabase);
string strWeekNode = "W" + dateMonday.ToString("yyyyMMdd");
List<string> listNames = new List<string>();
var result = docAssignHistory.Descendants(strWeekNode);
if(result != null)
{
foreach (var item in result)
{
listNames.Add(item.Descendants("Chairman").First().Value);
listNames.Add(item.Descendants("AuxCounsellor1").First().Value);
listNames.Add(item.Descendants("AuxCounsellor2").First().Value);
listNames.Add(item.Descendants("VideoConferenceHost").First().Value);
listNames.Add(item.Descendants("VideoConferenceCohost").First().Value);
listNames.Add(item.Descendants("PrayerOpen").First().Value);
listNames.Add(item.Descendants("PrayerClose").First().Value);
listNames.Add(item.Descendants("CBSConductor").First().Value);
listNames.Add(item.Descendants("CBSReader").First().Value);
var result2 = item.Descendants("Items").First();
if(result2 != null)
{
var result3 = result2.Descendants("Name");
foreach(var item2 in result3)
{
listNames.Add(item2.Value);
}
}
}
}
return listNames.ToArray();
}
Take for example:
listNames.Add(item.Descendants("Chairman").First().Value);
I want to change all of these lines so that the name is only added if it is not empty. I know I can save the value to a string
and then test the string value and add it based of the if
result. But is there a more conpact way?