在什么时候做LINQ到SQL或LINQ发送请求到数据库 [英] at what point does linq-to-sql or linq send a request to the database
问题描述
我想让我的疑问更好,但一直未能够找到一个资源在那里它规定了当一个查询被运到分贝。
的DbContext DB =新的DbContext();
为了_order =(邻以dB为单位
其中,o.OrderID ==QWERTY键盘ASDF-xcvb
选择o).FirstOrDefault();
字符串_custName = _order.Customer.Name ++ _order.Customer.Surname;
是否_custName的分配需要对数据库的任何请求?
请问的
分配_custName
需要对数据库的任何请求?
块引用>这取决于是否
Order.Customer
被延迟加载。如果延迟加载,那么是的。否则,没有。顺便说一句,你可以很容易,如果你设置
DataContext.Log
财产调查这样的:db.Log = Console.Out;
然后你可以看在控制台上的SQL语句。通过你的程序步进你可以清楚地看到,当SQL语句访问数据库。
查看MSDN上延迟与即刻负重。特别是,您可以关闭延迟加载。当心
SELECT N + 1
问题。I want to make my queries better but have been un-able to find a resource out there which lays out when a query is shipped of to the db.
DBContext db = new DBContext(); Order _order = (from o in db where o.OrderID == "qwerty-asdf-xcvb" select o).FirstOrDefault(); String _custName = _order.Customer.Name +" "+_order.Customer.Surname;
Does the assignment of _custName need to make any request to the database?
解决方案Does the assignment of
_custName
need to make any request to the database?It depends on whether or not
Order.Customer
is lazily loaded. If it is lazily loaded, then yes. Otherwise, no.By the way, you can investigate this easily if you set the
DataContext.Log
property:db.Log = Console.Out;
Then you can watch the SQL statements on the console. By stepping through your program you can see exactly when the SQL statement hits the database.
Check out MSDN on Deferred versus Immediate Loading. In particular, you can turn off lazy loading. Watch out for the
SELECT N + 1
problem.这篇关于在什么时候做LINQ到SQL或LINQ发送请求到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!