如何使用LINQ在C#中连接两个List<string>?

3

从SharePoint Web服务返回的XML元素z:row中有两个不同的国家属性。

var homeCountry = (from xml in doc.Descendants(z + "row") select xml.Attribute("ows_Country").Value).Distinct();

    var covCountry = (from xml in doc.Descendants(z + "row")
                      where xml.Attribute("ows_Coverage_x0020_Area_x0020_by_x00").Value != ""
                      select xml.Attribute("ows_Coverage_x0020_Area_x0020_by_x00").Value);

现在我想合并这两个列表,以获取不同的国家名称并加载下拉框。
distinctCountriesList.Add("");
        distinctCountriesList.Sort();
        country.DataSource = distinctCountriesList.Distinct();

        country.DataBind();

尝试在homeCountry和covCountry上进行内连接。 - Ali Hasan
4个回答

6
var distinctCountriesList = homeCountry.Union(covCountry).ToList();

3
country.DataSource = homeCountry
    .Union(convCountry)
    .ToList();

country.DataBind();

在这个例子中,.Distinct() 是多余的。.Union() 保证唯一性。 - recursive

2
使用Union方法。相比之下,Concat不会过滤重复项。

0
var Joinedlist = from hCountry in homeCountry.AsEnumerable()
                 join coCountry in covCountry.AsEnumerable()
                 on coCountry.<column Name> equals hCountry.<column Name>     

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接