I am returning two lists from the database using LINQ to SQL compiled query.
While looping the first list I remove duplicates from the second list as I dont want to process already existing objects again.
eg.
//oldCustomers is a List<Customer> returned by my Compiled Linq to SQL Statmenet that I have added a .ToList() at the end to
//Same goes for newCustomers
for (Customer oC in oldCustomers)
{
//Do some processing
newCustomers.Remove(newCusomters.Find(nC=> nC.CustomerID == oC.CusomterID));
}
for (Cusomter nC in newCustomers)
{
//Do some processing
}
DataContext.SubmitChanges()
I expect this to only save the changes that have been made to the customers in my processing and not Remove or Delete any of my customers from the database.
Correct?
I have tried it and it works fine – but I am trying to know if there is any rare case it might actually get removed
Right. When you call
.ToList()extension method on any IEnumerable, a new in-memory list of such items is created, without any binding to the previous location of items.You can add or remove items to/from such a list without fear of some side-effects.
But I have to add that your code is terrible performance-wise.
for (Cusomter nC in newCusomters.Except(oldCustomers))is much faster and easier to write.