仅当列表不为空时才将字符串添加到列表中(来自 XElement)

Adding a string to a list only if it is not empty (from XElement)

示例代码:

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();
}

举个例子:

listNames.Add(item.Descendants("Chairman").First().Value);

我想更改所有这些行,以便仅在名称不为空时才添加该名称。我知道我可以将值保存到 string,然后测试字符串值并根据 if 结果添加它。但是有没有更简洁的方式呢?

根据评论中关于使用本地函数的建议,我想出了这个:

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();
}