Depending on how you are using the elements (and how many there are), you may find it much better to load all entities at once:
// given List<int> idList
var tasks = _model.Tasks.Where(x => idList.Contains(x.id))
foreach (var task in tasks)
{
Save(task, task.id);
}
Notice that Find
performs a Where
with the Primary Key, so you have to be careful if the entity does not have a Primary Key or if it has a composite one. You also don't get any compile-time safety of the type of the key (i.e you could pass a string
to an entity with an int
PK and find out at only run-time).
Of course, if the list is big enough, you may run into an OutOfMemoryException
by bringing all data at once.