LINQ to SQL 不返回数据,但生成的 SQL 语句会返回数据
我有一个 LINQ to SQL 查询,执行时不会返回任何数据。但是,当我调试时,我可以获取生成的 SQL 查询,插入变量的值,然后在 SQL Management Studio 中运行它以获得我期望的记录。我也在调试模式下获取变量的值。
以前有人经历过这样的事情吗?
根据要求,LINQ 语句(编辑表和列名称):
var q1 = from rr in db.ABC
from rd in db.DEF
where rr.a == rd.b
where rr.c == rd.c
where rr.d.Equals(id)
where rr.c.Equals(anotherId)
select new
{
rr.d, rr.x, rr.a,
rr.y, rr.z, rr.v,
rr.e, rd.r
};
var r1 = q1.Single();
我在其他地方成功地使用了非常类似的查询。
I have a LINQ to SQL query that when executed does not return any data. However, when I debug, I can take the generated SQL query, plug in the values for the variables, and run it in SQL Management Studio to get the record I'm expecting. I'm taking the values for the variables while in debug mode as well.
Has anyone experienced something like this before?
As requested, LINQ statement (edited table and column names):
var q1 = from rr in db.ABC
from rd in db.DEF
where rr.a == rd.b
where rr.c == rd.c
where rr.d.Equals(id)
where rr.c.Equals(anotherId)
select new
{
rr.d, rr.x, rr.a,
rr.y, rr.z, rr.v,
rr.e, rd.r
};
var r1 = q1.Single();
I'm using very similar queries in other places with success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否运行了 Profiler 以确保您认为该语句将具有的值实际上位于发送到服务器的语句中?
Have you run Profiler to make sure the values you think the stament is going to have are actually inthe statment that was sent to the server?
我会仔细检查 LINQ to SQL 正在使用的连接字符串。它可能只是访问了错误的数据库。
I would double check the connection string that LINQ to SQL is using. It might just be hitting the wrong database.
运行 SQL Server 配置文件,获取正在运行的精确查询(您不必“插入”任何值),然后运行它。
您仍然获得结果吗?
插入的值是否正确?
您是否正在查询可为 null 的值?< br>
您能向我们展示代码和生成的查询吗?
Run the SQL Server profile, take the EXACT query it's running (you shouldn't have to "plug in" any values), and run that.
Are you still obtaining results?
Is it plugging in the correct values?
Are you querying a nullable value?
Could you show us the code and generated query?