I insert a new object into LINQ-to-SQL DataContext without calling SubmitChanges() yet:
MyDataContext db = new MyDataContext();
MyObject newObject = new MyObject()
{
Id = 1,
Name = "MyName"
};
db.MyObjects.InsertOnSubmit(newObject);
Now in another place in my code I want to retrieve this new object, even though it is not in the database yet. So I pass the same DataContext instance there, because I believe that the new object is cached inside it. And now I want to retrieve it. But this doesn't work:
MyObject newObject = db.MyObjects.Where(o => o.Id == 1).SingleOrDefault();
How can I do what I want? Is that possible?
Best Answer
Try:
You'd think this would be the same, but see these two connect issues:
If that doesn't work, I know that the
.Single(o => o.Id == 1)
works in 3.5 SP1 (I don't know if I tried.SingleOrDefault(pred)
). Apparently the.Where(o => o.Id == 1).Single()
is fixed in 4.0 - again, I haven't tried.Where(pred).SingleOrDefault()
.