我正在尝试获取我的linq语句,以便在两个日期之间获取所有记录,而且我不太确定需要更改才能使其工作: (a.Start >= startDate && endDate)
var appointmentNoShow =
from a in appointments
from p in properties
from c in clients
where a.Id == p.OID && (a.Start.Date >= startDate.Date && endDate)
Run Code Online (Sandbox Code Playgroud)
Gio*_*rgi 133
只需将其更改为
var appointmentNoShow = from a in appointments
from p in properties
from c in clients
where a.Id == p.OID &&
(a.Start.Date >= startDate.Date && a.Start.Date <= endDate)
Run Code Online (Sandbox Code Playgroud)
And*_*air 24
var appointmentNoShow = from a in appointments
from p in properties
from c in clients
where a.Id == p.OID
where a.Start.Date >= startDate.Date
where a.Start.Date <= endDate.Date
Run Code Online (Sandbox Code Playgroud)
var QueryNew = _context.Appointments.Include(x => x.Employee).Include(x => x.city).Where(x => x.CreatedOn >= FromDate).Where(x => x.CreatedOn <= ToDate).Where(x => x.IsActive == true).ToList();
Run Code Online (Sandbox Code Playgroud)
所以你正在向下滚动,因为答案不起作用:
这就像魔术一样(但他们说它对大数据有效率问题,而且你和我一样不关心)
1-在我的例子中,数据库中的数据类型是“日期时间”和“可为空”。
DB 中的示例数据格式如下:
2018-11-06 15:33:43.640
Run Code Online (Sandbox Code Playgroud)
C# 中的 An 转换为字符串时如下所示:
2019-01-03 4:45:16 PM
Run Code Online (Sandbox Code Playgroud)
所以格式是:
yyyy/MM/dd hh:mm:ss tt
Run Code Online (Sandbox Code Playgroud)
2-因此您需要首先以正确的格式准备日期时间变量:
实施例1
yourDate.ToString("yyyy/MM/dd hh:mm:ss tt")
Run Code Online (Sandbox Code Playgroud)
示例 2 - 过去 30 天的日期时间范围
DateTime dateStart = DateTime.Now.AddDays(-30);
DateTime dateEnd = DateTime.Now.AddDays(1).AddTicks(-1);
Run Code Online (Sandbox Code Playgroud)
3-最后你花了一天时间寻找的 linq 查询(需要 EF 6)
using System.Data.Entity;
_dbContext.Shipments.Where(s => (DbFunctions.TruncateTime(s.Created_at.Value) >= dateStart && DbFunctions.TruncateTime(s.Created_at.Value) <= dateEnd)).Count();
Run Code Online (Sandbox Code Playgroud)
还要考虑时间比较:
(DbFunctions.CreateDateTime(s.Created_at.Value.Year, s.Created_at.Value.Month, s.Created_at.Value.Day, s.Created_at.Value.Hour, s.Created_at.Value.Minute, s.Created_at.Value.Second) >= dateStart && DbFunctions.CreateDateTime(s.Created_at.Value.Year, s.Created_at.Value.Month, s.Created_at.Value.Day, s.Created_at.Value.Hour, s.Created_at.Value.Minute, s.Created_at.Value.Second) <= dateEnd)
Run Code Online (Sandbox Code Playgroud)
请注意,其他 stackoverflow 问题和答案中提到的以下方法将无法正常工作:
....
&&
(
s.Created_at.Value.Day >= dateStart.Day && s.Created_at.Value.Day <= dateEnd.Day &&
s.Created_at.Value.Month >= dateStart.Month && s.Created_at.Value.Month <= dateEnd.Month &&
s.Created_at.Value.Year >= dateStart.Year && s.Created_at.Value.Year <= dateEnd.Year
)).count();
Run Code Online (Sandbox Code Playgroud)
例如,如果开始日是本月,结束日是下个月,则查询将返回 false 并且没有结果,例如:
我们想要的数据库创建的项目 = 2018/12/05
开始日期 = 2018/12/01
结束日期 = 2019/01/04
查询将始终搜索 01 到 04 之间的日期,而不考虑“月”,因此“s.Created_at.Value.Day <= dateEnd.Day”将失败
如果您有非常大的数据,您将执行本机 SQL 查询而不是 linq
...
... where Shipments.Created_at BETWEEN CAST(@Created_at_from as datetime) AND CAST(@Created_at_to as datetime))
....
Run Code Online (Sandbox Code Playgroud)
谢谢
归档时间: |
|
查看次数: |
243329 次 |
最近记录: |