LINQ Object Persistance
https://stackoverflow.com/questions/928046
Question
Using the following code here
Sub FooSub
Dim db As New FooDataContext
Dim u = From p In db.FooTable Where p.FooColumn = FooData Select p
Do Stuff
End Sub
Do I have to manually create a class to map the data from my FooTable if I want it to persist in the application? If I declare u as a class-wide variable, then once SubFoo ends, I can no longer access the data in u
Solution
Actually, u
will only be IQueryable<typeof(p)>
. The database won't even be hit until you try to iterate over that collection.
If you want some objects to work with after the DataContext connection has closed, you'll want to call u.ToList
before you close the connection to get a list of the results.
From there, you can change the objects themselves as you like. If you want changes to persist back to the database, call db.SubmitChanges
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow