开发者

How to identify set of records to be deleted using LinqToSql where not in another set?

I have a daily XML feed which contains all records each day. These need to be processed and loaded to a database i.e. those not included in the feed for day2 which were in day1 feed should be deleted and any differences should cause an update on the database. My current approach is to update each included record with a processed date and then delete all records not processed that day.

This however needs each record to be updated each day, and I wondered if there was a better way?

I currently load the XML file and find within my dbcontext each record which currently exists for each record within the xml as follows:

return (from venue in db.Venues 
where venue.id = xmlVenueId
select venue).FirstOrDefault();

if one exists, then I update all of the fields with the current xml file information and if one doesn't exist I create a new one. If al开发者_开发技巧l of the fields are the same, then the database is not updated.

When I have processed all records I then need to delete any records which had not been affected.

How should I form a list of all of the venues found (should I extract the primary keys and then add to a List or take the whole Venue) and what would linq2Sql query look like to find all records not in this list?


Not sure that it is the best way, but it seems to work:

I create a List<int> venuePrimaryKeys then perform the following query:

from venue in db.Venues
where ! venuePrimaryKeys.Contains(venue.id)
select venue

I can then traverse the resultant IQueryable and DeleteOnSubmit each venue in the resultant list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜