I’m familiar with .NET and with SQL. Now I’m looking at the new LINQ and it looks to me just like a cursor. I understand the ease of use, etc., but if I do a LINQ-to-SQL query with a foreach loop, am I just using a DB cursor? Or is there some sort of magic behind the scenes where LINQ collects all the data at once and feeds it to my program one row at a time?
Share
No it’s not a cursor in the SQL sense. It doesn’t iterate over the rows in the database one at a time. Rather, it dynamically constructs a query using the conditions that you specify. It delays the execution of the query until the first data is needed, then executes the query against the database. Depending on how you consume the data, the data may be buffered for you so that you can iterate over it row by row or it could return the entire rowset as a collection, if you use the extension methods to obtain a list, array, etc. As far as the database is concerned, however, it is simply executing a SQL statement.