2011-11-28 36 views
2

從SharePoint Web服務返回的XML元素z:行中有2個單獨的國家/地區屬性。如何在使用LINQ的c#中加入兩個列表<string>

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

現在我想合併兩個列表,以獲得不同的國家名稱並加載dropdow dox。

distinctCountriesList.Add(""); 
     distinctCountriesList.Sort(); 
     country.DataSource = distinctCountriesList.Distinct(); 

     country.DataBind(); 
+0

試穿homeCountry和covCountry –

回答

5
var distinctCountriesList = homeCountry.Union(covCountry).ToList(); 
2

使用Union method。相反,Concat不會過濾重複項。

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

country.DataBind(); 
+0

內加入'.Distinct()'是在本例中是多餘的。 '.Union()'保證唯一性。 – recursive

+0

你是對的... –

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