I am having trouble determining if a record exists in the database table but not in the updated records contained within a list. If the record does not exist within the updated list but does within the database table then that record needs to deleted.
I can query the table to determine if it has been modified and if the record does not exist in the database table then update it, but determining the opposite is proving a bit more tricky. Another constraint is that I need the outcome to be a bool value of exists = true and not exist = false so that i can than handle the deleting of the record appropriately.
I am doing this to determine if the record does not exist in the database table:
bool exists = db.table.Any(t => t.EntityID != list.EntityID)
But can not seem to manage the reverse.
The list and the database table have 1 to 1 mapping using Entity Framework.
If record exists in database but not in list then delete.
Looks like you've reversed the logic there. If you want to check for the existence of a particular record, the ID of which is stored in
list.EntityID, it looks like this:
bool exists = db.table.Any(t => t.EntityID == list.EntityID);
If you have a list of entities and you want to find any records that exist in the database that don't exist in the list you can do this:
var extraitems = db.table.Where(t => !list.Select(l => l.EntityID).Contains(t.EntityID));
That will give you a list of entities you can pass to
bool exists = db.table.Any(t => list.EntityID.Contains(t.EntityID))