Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created March 14, 2021 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xximjasonxx/ae02a5a8f242798c4c16ff2f5cb2c496 to your computer and use it in GitHub Desktop.
Save xximjasonxx/ae02a5a8f242798c4c16ff2f5cb2c496 to your computer and use it in GitHub Desktop.
// variant 1
public async Task<bool> DoWork(IList<SomeItem> items)
{
// assume _context is our EF Context
foreach (var item in items.Where(x => x.Id % 2 == 0))
{
var it = await _context.FirstOrDefaultAsync(x => x.Id == item.Id);
_context.Remove(it);
}
await _context.SaveChangesAsync();
}
// variant 2
public async Task<bool> DoWork(IList<SomeItem> items)
{
// assume _context is our EF Context
var targetItems = await _context.Items.Where(
x => items.Where(x => x.Id % 2 == 0).Contains(x.Id)).ToListAsync());
foreach (var item in targetItems)
{
_context.Remove(item);
}
await _context.SaveChangesAsync();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment