如何将此 sql 部分转换为 linq


select sum(DATEDIFF(day,LeaveBreakup.StartDate,LeaveBreakup.EndDate)+1) 

我想要的是将语句转换为 linq select 语句

class LeaveBreakup
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}
List<LeaveBreakup> Dates = new List<LeaveBreakup>();
Dates.Add(new LeaveBreakup(){StartDate = DateTime.Now.AddDays(-3), EndDate = DateTime.Now });
Dates.Add(new LeaveBreakup(){StartDate = DateTime.Now.AddDays(-2), EndDate = DateTime.Now });

林克位

var Result = (from D in Dates
                select (D.EndDate - D.StartDate).TotalDays + 1)
            .Sum();

如果您想知道差异而不必担心负值,请将计算包装在 Math.Abs 中

var Result = (from D in Dates
                select Math.Abs((D.StartDate - D.EndDate).TotalDays) + 1)
            .Sum();

在此示例中,结果为 7

相关内容

  • 没有找到相关文章

最新更新