Linq to SQl Deferred or not
One of the most peculiar characteristic of Linq is Deferred execution, here is an example.
|
|
This sample create a linq to sql query to extract all customer from northwind database that has contact name that starts with Maria, then modify the record with ID=ALFKI and iterate again in the result, here is the output.
|
|
I’ve shortened the Sql clause, but you can see that each time you iterate for customer in result query, linq to sql made another query to the database. This is clear from the result, the first time the query return two record, ALFKI and FOLKO, the second time the ALFKI record is not returned because data in database is changed and does not match the query anymore. But pay attention because not everything is deferred in linq to sql. Take the example and change the query in this way
|
|
Now you are selecting customers that starts with “A”, the rest of the code is the same, print record, change contact name of ALFKI and then print again result, here is the output.
|
|
As you can see, when you iterate for the second time the query gets executed again, but the contact name of ALFKI is not changed. This happens because when the LINQ to SQL execute the query the second time, all the object are already constructed in memory, so all the result of the query gets ignored.
This can be a surprising behavior but is correct because objects that are already in the context does not gets update when you iterate again, even if linq to sql issue another query to the database. Only the “Where” part of the query is really deferred.
alk.
Tags: Linq To Sql