0
for (int i = 0; i < skus.Count; i++)
{
    sku item = new sku();
    item = skus[i];
    sku sku = CompanyDbContext.skus.Where(s => s.item_no == item.item_no).FirstOrDefault();

    if (sku == null) // ok to insert [no duplicate item numbers]
    {
        CompanyDbContext.skus.Add(item);                             
    }

}
CompanyDbContext.SaveChanges();

I'm getting

collection was modified enumeration operation may not execute

error. How can I fix this ?

Ravi Rajindu
  • 380
  • 2
  • 6
  • 23

2 Answers2

0

The problem is that you are modify the same thing that you are iterating. As best practice you should update your method something like this:

//get search predicat from List<sku> skus
var item_nos = skus.Select(s=>s.item_no).ToList();
//items already in repo
var addedItems = CompanyDbContext.skus.Where(s => item_nos.Contains(s.item_no)).ToList();
var newItems = skus.Except(addedItems).ToList();
foreach(var sku in newItems){
    CompanyDbContext.skus.Add(item);  
}
CompanyDbContext.SaveChanges();
ColinM
  • 2,622
  • 17
  • 29
Ionut N
  • 434
  • 4
  • 9
0

As mentioned in the comments, this happens because you are modifying the collection which you are looping through as you're performing your work.

One option you have is to create a temporary collection and add your sku items to that, and finally add the contents of the temporary List<sku> to your CompanyDbContext

// Create a new temporary list
List<sku> tempSkus = new List<sku>();
for (int i = 0; i < skus.Count; i++)
{
    // Let's assign item to skus[i] immediately, we don't need a new instance here when we're later re-pointing to an existing instance
    sku item = skus[i];

    // Use LINQ Any function to determine whether there are any existing SKU's already
    bool existingSku = CompanyDbContext.skus.Any(s => s.item_no == item.item_no);

    // There are no duplicates, let's add this sku item to our temporary List
    if(!existingSku) 
    {
        tempSkus.Add(item);
    }
}
// Add the Range of tempSkus List to the CompanyDbContext
CompanyDbContext.skus.AddRange(tempSkus);
CompanyDbContext.SaveChanges();

Or if you prefer LINQ

// Create a new temporary list
List<sku> tempSkus = skus.Where(p => CompanyDbContext.skus.Any(s => s.item_no != p.item_no)).ToList();
// Add the Range of tempSkus List to the CompanyDbContext
CompanyDbContext.skus.AddRange(tempSkus);
CompanyDbContext.SaveChanges();
ColinM
  • 2,622
  • 17
  • 29