Created
March 14, 2021 18:43
-
-
Save xximjasonxx/ae02a5a8f242798c4c16ff2f5cb2c496 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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