0

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?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 2
    Write a local method that does it, so you're not duplicating code? Then you can just have `MaybeAdd("Chairman"); MaybeAdd("AuxCounsellor1");` etc. – Jon Skeet Mar 28 '22 at 16:29

1 Answers1

0

Based on the suggestion in the comments about using a local function, I have come up with this:

public string[] GetMeetingPersonnel(DateTime dateMeeting, string strHistoryDatabase)
{
    void AddPersonnel(XElement item, string strAssignment, List<string> listNames1)
    {
        string strName = item.Descendants(strAssignment).First().Value;
        if(strName != string.Empty)
        {
            listNames1.Add(strName);
        }
    }

    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)
        {
            AddPersonnel(item, "Chairman", listNames);
            AddPersonnel(item, "AuxCounsellor1", listNames);
            AddPersonnel(item, "AuxCounsellor2", listNames);
            AddPersonnel(item, "VideoConferenceHost", listNames);
            AddPersonnel(item, "VideoConferenceCohost", listNames);
            AddPersonnel(item, "PrayerOpen", listNames);
            AddPersonnel(item, "PrayerClose", listNames);
            AddPersonnel(item, "CBSConductor", listNames);
            AddPersonnel(item, "CBSReader", listNames);

            var result2 = item.Descendants("Items").First();
            if(result2 != null)
            {
                var result3 = result2.Descendants("Name");
                foreach(var item2 in result3)
                {
                    if(item2.Value != string.Empty)
                    {
                        listNames.Add(item2.Value);
                    }
                }
            }
        }
    }
    return listNames.ToArray();
}
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • Why are you using `ref`? – Jon Skeet Mar 28 '22 at 16:41
  • @JonSkeet I have removed the `ref` keyword. All good. – Andrew Truckle Mar 28 '22 at 17:26
  • 1
    I'd also note that if you move the local method to the *bottom* (or anywhere under the `listNames` declaration) then you don't need to pass `listNames` to the method. You could do the same with `item` by declaring the local method within the loop, but personally I try to avoid that. – Jon Skeet Mar 29 '22 at 05:40