从星期几中选择数据

时间:2018-01-25 11:59:44

标签: c# sql-server entity-framework

我想在星期几之间选择数据。 一个例子是在下列时间之间选择所有数据:

  • 星期一10:00-16:00
  • 星期二3:00-18:00
  • 星期三15:00-18:00
  • 周四13:00-19:00
  • 星期五13:00-19:00
  • 星期六13:00-19:00
  • 星期日13:00-19:00

日期在数据库中标记为UTC,我想要一个可以选择时区的解决方案。

如何使用C#/ Entity Framework / MSSQL选择这些数据?

以下是我按周时间获取数据的MSSQL查询示例,但这不考虑时区

select 
    cast(AVG(cast(success as float)*100) as decimal(18,2)) Avg,
    convert(date,time) Time
from 
    recordings
where 
    Id = @Id
    and time >= @startDate 
    and time <= @endDate
    AND (
        (CAST(time AS TIME) BETWEEN @mondayStart AND @mondayEnd AND DATENAME(DW, time) = 'Monday')
        OR (CAST(time AS TIME) BETWEEN @tuesdayStart and @tuesdayEnd AND DATENAME(DW, time) = 'Tuesday')
        OR (CAST(time AS TIME) BETWEEN @wednesdayStart and @wednesdayEnd AND DATENAME(DW, time) = 'Wednesday')
        OR (CAST(time AS TIME) BETWEEN @thursdayStart and @thursdayEnd AND DATENAME(DW, time) = 'Thursday')
        OR (CAST(time AS TIME) BETWEEN @fridayStart and @fridayEnd AND DATENAME(DW, time) = 'Friday')
        OR (CAST(time AS TIME) BETWEEN @saturdayStart and @saturdayEnd AND DATENAME(DW, time) = 'Saturday')
        OR (CAST(time AS TIME) BETWEEN @sundayStart and @sundayEnd AND DATENAME(DW, time) = 'Sunday')
    )
group by 
    convert(date, time)
order by 
    convert(date, time)

1 个答案:

答案 0 :(得分:-1)

var result = context.YourEntityName.Where(entry => entry.DateField >= minDate && entry.DateField <= maxDate).ToList();