title: How to delete multiple rows in ASP .NET Core from a table in relation one-to-many at once?
body: <p>I am trying to delete several records from the <code>Locations</code> table at once. However, I get an exception after the second pass of the foreach loop. There is also a <code>RemoveRange</code> method, but it takes <code>index</code> and <code>count</code>, but in this case, I don't have these parameters to use this method. How can I in this case remove several items at once from a table that is in relation one-to-many?</p>
  <pre><code>public async Task RemoveMultipleLocations(int id, IEnumerable&lt;int&gt; locationsIds)
  {
     var profile = await context.AlertProfiles.Include(x =&gt; x.Locations).FirstOrDefaultAsync(x =&gt; x.Id == id);
  
     var existing = profile.Locations.Where(x =&gt; locationsIds.Contains(x.Id)).ToList();
     if(existing != null)
     {
           foreach(var ex in existing)
           {
                profile.Locations.Remove(ex);
           }
           profile.ModifiedDate = DateTimeOffset.Now;
           await context.SaveChangesAsync();
     }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/336cr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/336cr.jpg" alt="RemoveRange" /></a></p>