I need a way to determine which rows were deleted from the DB, given a list of IDs of interest.
Something like:
var idsOfInterest = new int[] {1,2,3,4};
idsOfInterest.Except(Datacontext.Table.Select(tbl=>tbl.ID)).Dump();
How can I do that? What’s an optimal LINQ query for it?
EDIT: Is it possible to do it in one step(a single DB query), without querying the existing IDs and then applying Except?
Your own code will do fine. However, there might be a performance problem since you are pulling all IDs from the database, so the following is more efficient.
UPDATE:
From your update I understand, you would like to retrieve all missing IDs from the database, which is quite hard to do, since you want the IDs that don’t exist anymore from the database.
If you would try to do this with plain old SQL, you would need either a table that contains a big list of numbers { 0, 1, 2, 3, … } to join against (since your
Tabledoes not contain all IDs anymore) or you would create an inner query or table valued function). That might look like this:As far as I know, there is no way to translate such construct to LINQ to SQL. The same holds for table valued functions.
So what you can do is join your
Tablewith some sort ofNumberstable, but I personally wouldn’t do that, since you’re just over complicating things.