Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何选择/合并多个匹配结果?_C#_Asp.net_Linq - Fatal编程技术网

C# 如何选择/合并多个匹配结果?

C# 如何选择/合并多个匹配结果?,c#,asp.net,linq,C#,Asp.net,Linq,我正在尝试创建一组级联的下拉列表,即基于区域列表的国家列表 我在代码隐藏中创建了一个词典,其中包括一个地区、国家/地区列表 现在在区域下拉选择(可多选)上, 我需要选择属于特定区域(选定区域)的国家,并将其绑定到国家列表 我试着这样做: List<string> selectedRegions = (from ListItem item in regionList.Items where item.Selected

我正在尝试创建一组级联的下拉列表,即基于区域列表的国家列表

我在代码隐藏中创建了一个
词典
,其中包括一个地区、国家/地区列表

现在在区域下拉选择(可多选)上, 我需要选择属于特定区域(选定区域)的国家,并将其绑定到国家列表

我试着这样做:

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();
List selectedRegions=(来自regionList.Items中的ListItem项
其中项目。已选定
选择item.Text).ToList();
变量countryList=(选定的地区)
.Where(item=>regionToCountry.ContainsKey(item))
.Select(item=>new{value=regionToCountry[item]});
countryList.DataSource=countryList.ToList();
countryList.DataBind();
问题是country list以索引格式获取结果,如:countryList[0](包含区域a中的所有国家) 来自区域B的国家列表[1]

我需要一个合并列表,我可以绑定到下拉列表

先谢谢你

Vishal

您可以使用它展平
词典中的
列表

您可以使用来展平
词典中的
列表

试试这个:

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

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

首先,这个如何编译
var countryList=。。。;countryList.DataSource=countryList.ToList()?听起来你可能需要
。选择many(…)
首先,这个如何编译
var countryList=。。。;countryList.DataSource=countryList.ToList()?听起来你可能需要
。选择许多(…)
美丽、坚韧,对我来说有点复杂:),但工作起来很有魅力。非常感谢。美丽,坚韧,对我来说有点复杂:),但工作起来很有魅力。谢谢。