如何在LINQ to SQL中减去两个日期?

时间:2016-08-02 17:54:04

标签: c# linq linq-to-sql asp.net-mvc-5

我希望能够将dd添加到custQuery7。 Aka,从trans_date中减去两个日期date_paid,其值大于3天。然后计算那些。

        var dd = m.date_paid.Value.Subtract(m.trans_date.Value) > 3;

        var custQuery7 = ((from m in DataContext.pu_balance_hists
                           where m.desc_code == 141
                           && m.trans_date.Value.Year == 2016
                           && m.trans_date.Value.Month == 5
                           select m)).Count();

这是我试图模仿的SQL查询:

select COUNT(*) as 'UnprocessedChques' from pu_balance_hist 
where desc_code=141 and date_paid-trans_date>3
and YEAR(trans_date)=2016 and MONTH(trans_date)=5

1 个答案:

答案 0 :(得分:2)

未经测试,但您可以执行以下操作:

var custQuery7 = ((from m in DataContext.pu_balance_hists
                       where m.desc_code == 141 
                       && SqlMethods.DateDiffDay(m.date_paid, m.trans_date) >= 3
                       && m.trans_date.Value.Year == 2016
                       && m.trans_date.Value.Month == 5
                       select m)).Count();

https://msdn.microsoft.com/en-us/library/bb468730(v=vs.110).aspx