简单 LINQ To 实体查询的问题
我有一个非常简单的 LINQ To Entities 查询,如下所示:
var orderID = 1;
var orders = (from o in db.Orders
where o.OrderID == orderID
select o).SingleOrDefault();
谁能告诉我为什么这个查询不起作用?它甚至不抛出异常。我还检查了 SQL 分析器,上面的查询甚至没有触发相应的 SQL 查询。但是,当我直接将 orderID 的值插入查询 where o.OrderID == 1 时,一切正常。
I have a very simple LINQ To Entities query as follows:
var orderID = 1;
var orders = (from o in db.Orders
where o.OrderID == orderID
select o).SingleOrDefault();
Can anyone tell me why this query wouldn't work? It doesn't even not throw an exception. I have also checked the SQL profiler and the above query does not even fire the corresponding SQL query. But when I directly plugin the value of the orderID into the query where o.OrderID == 1 then everything works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,一个更简单的查询是
其次,你确定你的查询没有在sql中触发吗?默认情况下,Linq 将尽可能推迟查询执行。这意味着只有当您调用 GetEnumerator 或对查询执行类似操作时,它才会执行查询。但是,在您的示例中, .SingleOrDefault() 调用应该执行查询。
检查以下内容:
Firstly, a much simpler query would be
Secondly, are you sure you query is not firing in sql? By default Linq will defer the query execution as late as possible. This means it will only execute a query when you call GetEnumerator or a similar operation on the query. However in your example the .SingleOrDefault() call should execute the query.
Check the following: