是否可以在Linq to Entities中进行DateAdd计算?

时间:2014-01-08 21:08:22

标签: linq-to-entities dateadd

我有以下sql,我不知道如何在Linq to Entities中执行DateAdd和IsNull。这甚至可能吗?

Select TOP 1 DateAdd(MINUTE, 
    (IsNull(AlarmMinutes, 0) * -1) + (IsNull(SnoozeMinutes, 0)), 
    DueDate) 
From Todo
Order By DateAdd(MINUTE, 
    (IsNull(AlarmMinutes, 0) * -1) + (IsNull(SnoozeMinutes, 0)), 
    DueDate) 

1 个答案:

答案 0 :(得分:1)

您可以使用EnitityFunctions.AddMinutes

var date = (from x in context.Todo
            let AlarmMinutes = x.AlarmMinutes.HasValue ? x.AlarmMinutes.Value : 0
            let SnoozeMinutes= x.SnoozeMinutes.HasValue ? x.SnoozeMinutes.Value : 0
            let newDate = EntityFunctions.AddMinutes(x.DueDate, AlarmMinutes*-1 + SnoozeMinutes)
            ordeby newDate
            select newDate).FirstOrDefault();