An error occurred while preparing the command definition.【求助】

oPingZi1234w 2014-05-14 02:50:15
求助:在查询linq绑定到控件时发生标题上所示异常,User和Title多对多关系,我跟踪代码,把linq生成的sql语句放到查询分析器里运行,是能运行的,不过关系表里没数据,所以查询结果是没有数据的空表。请问到底哪里出了问题。最后附上堆栈跟踪
   private void BindGrid2()
{
int titleID = GetSelectedDataKeyID(Grid1);

if (titleID == -1)
{
Grid2.RecordCount = 0;

Grid2.DataSource = null;
Grid2.DataBind();
}
else
{
IQueryable<User> q = DB.Users;

// 在用户名称中搜索
string searchText = ttbSearchUser.Text.Trim();
if (!String.IsNullOrEmpty(searchText))
{
q = q.Where(u => u.Name.Contains(searchText));
}

q = q.Where(u => u.Name != "admin");

// 过滤选中岗位下的所有用户(这句如果注释掉就没问题)
q = q.Where(r => r.Titles.Any(n => n.ID == titleID));

// 在查询添加之后,排序和分页之前获取总记录数
Grid2.RecordCount = q.Count();

// 排列和分页
q = SortAndPage<User>(q, Grid2);
try
{
Grid2.DataSource = q;
Grid2.DataBind();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}


}

堆栈信息:
在 MySql.Data.Entity.SelectStatement.Accept(SqlFragmentVisitor visitor)
在 MySql.Data.Entity.ExistsFragment.Accept(SqlFragmentVisitor visitor)
在 MySql.Data.Entity.BinaryFragment.Accept(SqlFragmentVisitor visitor)
在 MySql.Data.Entity.SqlGenerator.VisitAndReplaceTableName(SqlFragment sf, String oldTable, String newTable)
在 MySql.Data.Entity.SqlGenerator.FuseSelectWithInnerSelect(SelectStatement outer, SelectStatement inner)
在 MySql.Data.Entity.SqlGenerator.TryFusingSelect(InputFragment f)
在 MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type)
...全文
682 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
oPingZi1234w 2014-06-05
  • 打赏
  • 举报
回复
谢谢各位,我已经知道原因了,是MySql数据库的EntityFramework不支持linq的Any等方法。如果换成sqlserver数据库就好了
oPingZi1234w 2014-05-14
  • 打赏
  • 举报
回复
最后这里q是不等于null的,实际是没数据的。
q107770540 2014-05-14
  • 打赏
  • 举报
回复
q = SortAndPage<User>(q, Grid2); 打断点进这里看看 Grid2.DataSource = q; 最后这里的q内有数据么

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧