我正在使用国家DDL填充状态DDL。
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”的类型。
发布于 2012-08-25 15:25:38
您说您试图用给定国家的州名填充一个DropDownList,看起来这个逻辑就在您的foreach循环中。看起来您的foreach循环做了很多不必要的工作。
当它只需要找到其中一个国家的名字时,它就会遍历所有的国家名称。然后,它还会再次遍历寻找各州的国名。
您应该能够抛出整个foreach循环,而使用以下方法:
var query = ProfileMasterDAL.GetStatesByCountry(DropDownList1.SelectedItem.Text);
DropDownList2.DataSource = query;
DropDownList2.DataBind(); 您的GetStatesByCountry方法只返回属于传入的国家名称的状态,因此您应该能够调用它,只获取这些州的名称,然后将它们分配给您的DropDownList。
发布于 2012-08-25 06:47:26
试着改变:
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 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() ;
} https://stackoverflow.com/questions/12119811
复制相似问题