C
C#5mo ago
SWEETPONY

✅ is there any way to delete items from the database and return the deleted list in one request?

is there any way to delete items from the database and return the deleted list in one request? I can do something like this: DbContext.T.Where(entity => ..).ExecuteDelete(), but my business logic assumes that I will return items that were deleted I can do this: var deleted = DbContext.T.Where(entity => ..) DbContext.RemoveRange(deleted) but these are already two requests to database
8 Replies
douchebag
douchebag5mo ago
i dont think so base on your code you have 2 queries
Anton
Anton5mo ago
I don't think you can do that with EFCore You can with linq2db for sure
Anton
Anton5mo ago
No description
Anton
Anton5mo ago
btw only do two queries if you're in a transaction
SWEETPONY
SWEETPONYOP5mo ago
I think row sql is the best option here - delete from x where x.a = 1 returning * with DbContext.T.Where(entity => ..).ExecuteDelete() I can only delete but I need to return deleted items too
Henkypenky
Henkypenky5mo ago
it's important to understand what happens behind the scenes. If you retrieve the range ef core tracks them, so when you proceed to delete them efcore already knows where they are and what state they are in, so the deletion is already optmized. It's not a question of using less commands but a question of understanding why using 2 is fine might as well not use EFCore...
Anton
Anton5mo ago
A good option is generating a query with info from dbContext.Model and running a raw query
SWEETPONY
SWEETPONYOP5mo ago
thanks for helping
Want results from more Discord servers?
Add your server