Ok so I’m basically trying to determine which way is more efficient performance wise when checking if items exist in a database.
I’m using LINQ to SQL on WP7 with SQL Server CE.
I’m going to be importing multiple objects into the database. Now there is a pretty good possibility that some of those objects already exist in the database so I need to check each item as they come in and if its already there, skip it, otherwise add it.
There were two approaches that came to mind. The first was using a foreach and checking if an object exists in the db with the same name:
foreach(var item in items)
{
//Make individual call to db for every item
var possibleItem = /*SQL SERVER STATEMENT WITH WHERE CONDITION*/;
}
Making individual calls to the db though sounds pretty resource intensive. So the other idea was to do a full select on all the objects in the db and store them in the list. And then pretty much do the same concept with the foreach except now I don’t have to connect to the db, I have direct access to the list. What are your thoughts on these approaches? Is there a better way?
If you can easily sort your items you could make a select from the database and step through the list while reading through the database to determine which items are new. That should be much faster than multiple selects while preserving memory.
But if memory if of no concern, I’d probably just read all keys from the database into memory for simplicity.