I use database and LINQ to SQL in my Windows Phone application. I have a database table that stores objects Foo with some ids. There is also an external data source (Json data from server), I can create IEnumerable of Foo from this Json. When I get new data from server I want to "refresh" table - delete all rows with ids that do not exist in new collection, add all new rows and update all existing rows. What is the best way to do this?
Asked
Active
Viewed 343 times
0
-
1You can just call delete on the table then commit the entire collection of `IEnumberable
`. I doubt that's the best way but it's definitely the simplest. – evanmcdonnal Nov 26 '13 at 00:38 -
I do it this way at the moment. The problem is that I need to check differences between 'old' and 'new' records with the same id, it required writing a lot of code by hands. Thank you for the answer anyway. – Aleksei Petrenko Nov 26 '13 at 01:12
-
What's the requirement to check the diffs? Do you need to audit these? You could MERGE (aka Upsert) the new/updated rows and then delete the ones that don't match in a single transaction. – Stinky Towel Nov 26 '13 at 01:29
-
'Upsert' was a correct keyword to google) Thank you. http://stackoverflow.com/questions/637225/how-would-you-write-an-upsert-for-linq-to-sql – Aleksei Petrenko Nov 26 '13 at 01:43