S
S
shvedovea2019-04-02 11:40:42
SQL
shvedovea, 2019-04-02 11:40:42

C# EF SQL how to make a query?

Please tell me how to delete certain rows in the database if they are 1 day overdue.

public string value { get; set; }
        
 public DateTime date { get; set; }

Here is a structure, I use LINQ. It is necessary if the date is overdue by 1 day from the current time, then delete it. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jan, 2019-04-02
@shvedovea

Something like this: Class.Where(x => x.date < DateTime.Today.AddDays(-1));
Get all the objects and do what you need with them

M
MrDywar Pichugin, 2019-04-02
@Dywar

Through EF:
1) Get rows by condition from the database.
2) Remove received rows by calling the Remove/RemoveRange method on the repository or how you get them.
Yes - you have to load first to uninstall, this is a limitation of pure EF. The data is raced twice.
Through SQL:
1) Write a SQL script.
Minus - it is better to avoid SQL when using EF, because when renaming properties, tables - scripts will need to be searched and edited separately. They are more difficult to test, change, etc.
Through an extension for EF:
1) We write context.BulkDelete(customers, options => CONDITION) and that's it.
We are looking for analogues https://entityframework-extensions.net/bulk-delete, there are free ones for sure.
In this case, we use EF, do not write SQL, and the data is not chased twice, the code is cleaner, more understandable, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question