Hi,@muttBunch. Welcome to Microsoft Q&A.
You can use the following examples as reference
1.Inserting Data
Assume that insertList is the data you want to insert
MyDbContext myDbContext = new MyDbContext();
//Find data within 24 hours from the database
var databaseList = myDbContext.MyLogs.Where(p => p.LogTime < DateTime.Now && p.LogTime > DateTime.Now.AddDays(-1)).ToList();
//Insert new data and remove duplicates
var resultList = databaseList.Union(insertList).DistinctBy(p => p.Id).ToList();
//Writing to the database
myDbContext.RemoveRange(databaseList);
myDbContext.SaveChanges();
myDbContext.AddRange(resultList);
myDbContext.SaveChanges();
public partial class MyLog
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public DateTime LogTime { get; set; }
}
2.Delete data older than 24 hours
var deleteList = myDbContext.MyLogs.Where(p => p.LogTime < DateTime.Now.AddDays(-1)).ToList();
myDbContext.RemoveRange(deleteList);
myDbContext.SaveChanges();
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.