Home > database >  Update based on Find at another table
Update based on Find at another table

Time:10-12

As Find can't be used that way what is the best way to do something like that with pure LINQ?

dbContext.Exports
  .Where(x => x.ExportDate < DateTime.UtcNow.Add((dbContext.Configurations.Find(x.ConfigurationId).Timeout) 
    && x.anotherCondition == "xyz")
  .Update(x => new Export() { Status = "Failed" });

between table Configurations and Exports, there is no relation on EF data model.

CodePudding user response:

You can use a foreign key here:

[Table("Exports")]
public partial class Exports
{
      public long configurationId { get; set; }

      [ForeignKey("configurationId")]
      public virtual Configuration Configuration { get; set; }
}

Then your query will look like this:

dbContext.Exports
  .Where(x => x.ExportDate < DateTime.UtcNow.Add(x.Configuration.Timeout)) 
  .Where(x => x.anotherCondition == "xyz")
  .Update(x => new Export() { Status = "Failed" });
  • Related