无法将节点“ClientQuery”格式化为SQL执行

时间:2012-07-23 12:17:45

标签: linq linq-to-sql

DataClassesDataContext MyDB = new DataClassesDataContext();
var id = from wineT in MyDB.Wineries
         where wineT.wineryID==Convert.ToInt32(
             from w in MyDB.WineTypes
             where w.kind==ddlSorti.SelectedItem.Text
             select w.wineryID)
         select  wineT.name;

GridView1.DataSource = id;
GridView1.DataBind();

我有这个代码,我没有任何错误,但是当我启动应用程序时,我收到此错误“无法格式化节点'ClientQuery'以执行为SQL”。

1 个答案:

答案 0 :(得分:2)

您需要在子选择中添加FirstOrDefault()

var id = from wineT in MyDB.Wineries
         where wineT.wineryID==Convert.ToInt32(
             from w in MyDB.WineTypes
             where w.kind==ddlSorti.SelectedItem.Text
             select w.wineryID).FirstOrDefault()
         select  wineT.name;