我有2个表:winery和wineType(在wineType中,我具有酿酒厂的外键,称为wineryID)。我尝试获得所有生产葡萄酒的酒厂名称,例如客户从下拉列表中选择的名称。我有这个功能
public void ispolniLista()
{
DataClassesDataContext MyDB = new DataClassesDataContext();
var id = from wineT in MyDB.WineTypes where wineT.kind == ddlSorti.SelectedItem.Text select new { wineT.wineryID };
List<int> listaID = id as List<int>;
List<string> listaIminja = new List<string>();
try
{
foreach (int i in listaID)
{
var vname = from w in MyDB.Wineries where w.wineryID == i select new { w.name };
listaIminja.Add(vname.ToString());
}
lstVinarii.DataSource = listaIminja;
lstVinarii.DataBind();
}
catch (NullReferenceException err)
{
Console.Write(err.Message);
}
}
我没有结果可言,lstVinarii是空的。
最佳答案
编辑
注意:从您的代码中,从select中删除new,无需创建额外的匿名类型,这也是一个问题
为了避免评论错误,请这样写
var id = from wineT in MyDB.WineTypes
where wineT.kind == ddlSorti.SelectedItem.Text
select wineT.wineryID ;
从select中删除new,无需在下面的代码中创建相同的额外匿名类型
尝试
List<int> listaID = id.ToList<int>();
比删除foreach循环并这样写
var listaIminja= (from win MyDB.Wineries
where listaID .Contains( w.wineryID )
select w.name ).ToList();
lstVinarii.DataSource = listaIminja;
lstVinarii.DataBind();
关于c# - 查询未返回任何内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11611059/