只是想知道将以下SQL转换为LINQ到SQL的最佳(最快执行时间)方法是什么?

IF EXISTS (SELECT TOP 1 * FROM HealthIndicators WHERE LogDate = getdate())
     UPDATE HealthIndicators SET Timeouts = 32
ELSE INSERT INTO HealthIndicators (Timeouts, 32)


如果这是转贴,我深表歉意,但未能找到关于该主题的答案(我是新来的,请保持友好!)

最佳答案

var d = DateTime.Today; // or .Now
if (db.HealthIndicators.FirstOrDefault(h => h.LogDate == d) != null)
// or Any(h => h.LogDate == d)
{
    // update
}
else
{
    // insert
}

10-07 20:51