首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LINQ中的country DDL填充状态DDL

使用LINQ中的country DDL填充状态DDL
EN

Stack Overflow用户
提问于 2012-08-25 06:40:43
回答 2查看 344关注 0票数 0

我正在使用国家DDL填充状态DDL。

代码语言:javascript
复制
public static IEnumerable bindcountry()
{
    var countries = from c in getdata().Descendants(("country"))
                    orderby (string)c.Element("name")
                    select (string)c.Element("name");
    return countries;
}

public List<string> GetStatesByCountry(string CountryName)
{

    var query = from user in getdata().Descendants("country")
                where user.Element("name").Value == CountryName
                from t in user.Descendants("text")
                select t.Value;
    return query.ToList();
}

foreach (var VARIABLE in ProfileMasterDAL.bindcountry())
{
    if (VARIABLE.ToString().Contains(DropDownList1.SelectedItem.Text))
    {
        var query = from row in ProfileMasterDAL.bindcountry()
                    where row.(ProfileMasterDAL.GetStatesByCountrys(DropDownList1.SelectedItem.Text))
                    select row;

        DropDownList2.DataSource = query;
        DropDownList2.DataBind();
    }
}

问题是,我无法在这里定义WHERE子句和equals,我得到了一个错误:

找不到源类型‘System.Collection. implementation’的查询模式的实现。“哪里”找不到。考虑显式指定范围变量“row”的类型。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-25 15:25:38

您说您试图用给定国家的州名填充一个DropDownList,看起来这个逻辑就在您的foreach循环中。看起来您的foreach循环做了很多不必要的工作。

当它只需要找到其中一个国家的名字时,它就会遍历所有的国家名称。然后,它还会再次遍历寻找各州的国名。

您应该能够抛出整个foreach循环,而使用以下方法:

代码语言:javascript
复制
var query = ProfileMasterDAL.GetStatesByCountry(DropDownList1.SelectedItem.Text);
DropDownList2.DataSource = query;   
DropDownList2.DataBind();   

您的GetStatesByCountry方法只返回属于传入的国家名称的状态,因此您应该能够调用它,只获取这些州的名称,然后将它们分配给您的DropDownList

票数 1
EN

Stack Overflow用户

发布于 2012-08-25 06:47:26

试着改变:

代码语言:javascript
复制
public static IEnumerable bindcountry()     
{           
var countries = from c in getdata().Descendants(("country")) orderby (string)c.Element("name") select (string)c.Element("name");

return countries ;       
}

代码语言:javascript
复制
public static IList<string> bindcountry()     
{           
var countries = from c in getdata().Descendants(("country")) orderby (string)c.Element("name") select (string)c.Element("name");        

return countries.ToList() ;       
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12119811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档