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()代码>?听起来你可能需要。选择许多(…)
美丽、坚韧,对我来说有点复杂:),但工作起来很有魅力。非常感谢。美丽,坚韧,对我来说有点复杂:),但工作起来很有魅力。谢谢。