简体   繁体   English

Select 使用 LINQ 计算两个日期之间的天数

[英]Select Count days between two dates using LINQ

I need to get the number of holidays between two dates.我需要获取两个日期之间的假期数。 I have tried using the query below but I'm getting an incorrect count number.我尝试使用下面的查询,但我得到的计数不正确。

StartDate开始日期 EndDate结束日期
01/2/2022 01/2/2022 03/2/2022 03/2/2022
17/2/2022 17/2/2022 19/2/2022 19/2/2022
SELECT COUNT(*) FROM table
WHERE StartDate <= '02/2/2022' and EndDate >= '19/2/2022'

how I make this date => '02/2/2022' return count of day that between two dates from the first row from the table and get count day from the second row.我如何设置此日期 => '02/2/2022' 从表的第一行返回两个日期之间的天数,并从第二行获取天数。

The count must be 5 days.计数必须为 5 天。

The simplest way of doing it is to have a table that has one row per day for every day from eg year 2000 to year 2099, then you can:最简单的方法是有一个表,每天一行,例如从 2000 年到 2099 年,然后你可以:

SELECT COUNT(*) 
FROM
  cal           --your dates table
  INNER JOIN h. --your holiday table 
  ON cal.d BETWEEEN h.start AND h.end
WHERE
  cal.d BETWEEN '2022-02-02' AND '2022-02-19'

The join will produce 6 rows, the where trims it to 5, there is your count联接将产生 6 行,将其修剪为 5 行,这是您的计数

Generating the calendar table, or its equivalent (an arbitrary series of dates) is an exercise for the reader.. See something like this生成日历表或其等效项(任意一系列日期)是读者的练习。看到这样的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM