akash akash - 2 years ago 91
SQL Question

How to convert this sql part to linq

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

what I want is to convert the statement to linq select statement

Answer Source
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)

If you want to know the diff without having to worry about having a negative value then wrap the calculation in Math.Abs

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

In this example your Result is 7

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download