Microsoft Developer Network >
Forums Home
>
Archived Forums Forums
>
LINQ Project General
>
Query Data That Has Not Yet Been Persisted
Query Data That Has Not Yet Been Persisted
- Hello everyone. I am new to LINQ, and had a question about querying records that have been added to my data conxtext (via insertonsubmit) but not yet persisted to my sql database.
Here's the scenario:
I'm building a WPF application that handles basic case management. During the course creating a case, a user can add multiple persons. I would like to be able to add person objects to my data context and display the names of the newly added (but not yet inserted into the database) persons in a list box.
My current linq query to retrieve the person names will not return data unless it has been persisted to my database, which I understand is by design. However, is there a way to query the records that have been added to my data context but not yet persisted to my database?
Answers
- IEnumerable<Person> emp = dc.GetChangeSet().Inserts.Where(i => i.GetType() == typeof(Person)).Cast<Person>();
Kristofer - Huagati Systems Co., Ltd.
Cool tools for Linq-to-SQL and Entity Framework:
huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)- Marked As Answer byheasty Thursday, November 05, 2009 3:41 PM
- Kristofer's answer is correct, however it can be shortened using the OfType method , which will return an IEnumerable<T>:
dc.GetChangeSet().Inserts.OfType<Person>();
Document my code? Why do you think it's called "code"?- Marked As Answer byheasty Thursday, November 05, 2009 7:00 PM
All Replies
- IEnumerable<Person> emp = dc.GetChangeSet().Inserts.Where(i => i.GetType() == typeof(Person)).Cast<Person>();
Kristofer - Huagati Systems Co., Ltd.
Cool tools for Linq-to-SQL and Entity Framework:
huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)- Marked As Answer byheasty Thursday, November 05, 2009 3:41 PM
- Thank you! That was exactly what I needed!
- Kristofer's answer is correct, however it can be shortened using the OfType method , which will return an IEnumerable<T>:
dc.GetChangeSet().Inserts.OfType<Person>();
Document my code? Why do you think it's called "code"?- Marked As Answer byheasty Thursday, November 05, 2009 7:00 PM
- Thank you both. The next thing I will need to do is to join the entities in the change set to return some specific rows. Is there a good reference out there for this type of thing that either of you can point me to?
- What do you want to join them to? Something else in the change set, or something db-side?
Kristofer - Huagati Systems Co., Ltd.
Cool tools for Linq-to-SQL and Entity Framework:
huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)

