I have this bit of code:
LinqDataContext ctx;
MyRecord R = new MyRecord();
R.Title = "test";
ctx.AllRecords.InsertOnSubmit(R);
bool bExists = ctx.AllRecords.Any(r => r.Title == "test");
Note: I have not called SubmitChanges.
Why does bExists come back as false? Shouldn't Linq be able to see the inserted record?
See Linq cannot write values into DB, if submitchanges() not called.
And for second question, Yes, Linq cache objects before submitting. We can also get records which are in cache but not Submitted to database.
As you inserted record above, we can get above record from cache of datacontext as below:
First Get changeset from DataContext as:
System.Data.Linq.ChangeSet MySet = ctx.GetChangeSet();
After it, extract your record form Changeset:
MyRecord b = (MyRecord )MySet.Inserts.Last();
You will get MyRecord with Title as "test", Which you Inserted.
Hope this will Help.
I believe that
bool bExists = ctx.AllRecords.Any(r => r.Title == "test");
is an SQL query that the SQL server returns the result. So if you have not submitted, then the DB does not know anything about:
MyRecord R = new MyRecord();
R.Title = "test";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With