How Do LINQ TO SQL Stored Procedure Calls Work?

倾然丶 夕夏残阳落幕 提交于 2020-01-06 07:14:11

问题


I have a LINQ TO SQL Context that I have created that calls a stored proc. When I'm looping through the ISingleResult is it creating entities on the fly while the underlying DataReader reads the results or does it put every thing into Entities upfront before the function call returns.

Basically what is going on is I'm working with a stored proc that may sometimes return 10's of thousands of records while most of the time it only returns a few thousand records.

DatabaseDataContext context = new DatabaseDataContext();
var resultSet = context.MyStoredProc();

foreach (var result in resultSet)
{
    // some code here
}

Would that that load every thing into memory at once or would it be loaded one at a time while I loop through it??


回答1:


The stored procedure will get called when you enumerate the result, so the execution is deferred. However, it is not lazily loaded. Meaning, once you enumerate the result of your stored procedure, you will effectively be executing the stored procedure in it's entirety and all the results will be brought back.

If you are binding the results of your stored procedure to a data model class, which supports lazy loading on it's child elements, then you get lazy loading on that class' properties.



来源:https://stackoverflow.com/questions/2372288/how-do-linq-to-sql-stored-procedure-calls-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!