2013-10-18 51 views
0

我想創建一個基於區域列表的下拉列表,國家列表的級聯集。如何選擇/合併多個匹配結果?

我在後面的代碼中創建了一個Dictionary<String, List<String>>,其中包括一個地區,國家列表。

現在在區域下拉選擇(這是多選), 我需要選擇屬於特定區域(選定的)的國家,並將其綁定到國家名單。

我想是這樣的:

List<string> selectedRegions = (from ListItem item in regionList.Items 
           where item.Selected 
           select item.Text).ToList(); 

var countryList = (selectedRegions 
          .Where(item => regionToCountry.ContainsKey(item)) 
          .Select(item => new { value = regionToCountry[item] })); 

countryList.DataSource = countryList.ToList(); 
countryList.DataBind(); 

問題是國家列表得到的結果類似索引格式:countryList [0](包含所有國家從區域) countryList [1]從區域B.

我需要一個可以綁定到下拉列表的合併列表。

非常感謝。

維沙爾

+0

首先,請問這個編譯'VAR countryList = ...; countryList.DataSource = countryList.ToList();'? –

+1

聽起來像你可能需要'.SelectMany(.....)' – Arran

回答

2

您可以使用SelectMany拼合List<string>Dictionary<TKey,TValue>內。

var countryList = 
    regionToCountry.Where(x => selectedRegions.Contains(x.Key)) 
        .SelectMany(x => x.Value) 
        .ToList(); 
+0

美麗,強硬有點複雜,我理解:),但工作像一個魅力。非常感謝。 – Vishal

2

試試這個:

var countryList = selectedRegions 
          .Where(item => regionToCountry.ContainsKey(item)) 
          .SelectMany(item => regionToCountry[item]) 
          .ToList();