我正在尝试查询SQLite数据库。我有两个表Doctor
和Appointment
。Appointment
表有两列可以为空,Date
(类型为DateTime
(和Durata
(类型为TimeSpan
(表示约会的持续时间。
var date = DateTime.Parse(request.Date);
var filteredDoctors = await _context.Doctors
.Where(d => d.Specializare == request.Speciality)
.Include(d => d.Appointments)
.Where(x => x.Appointments.Any(a => (a.Date != null && a.Date.Value.Date == date.Date) &&
(a.Date != null && date >= a.Date.Value) &&
((a.Date != null && a.Durata != null) && date <= System.Data.Entity.DbFunctions.AddMinutes(a.Date.Value, (int)a.Durata.Value.TotalMinutes))))
.ToListAsync(cancellationToken);
该查询在没有最后一个"0"的情况下工作&">
&& date <= System.Data.Entity.DbFunctions.AddMinutes(a.Date.Value, (int)a.Durata.Value.TotalMinutes)
表达式a.Date.Value.Add(a.Durata.Value)
不起作用。我尝试了stackoverflow上的大多数解决方案,这是我最后一个版本。还有什么可以做的吗?
为了简化查询,我想选择所有在指定的date
不忙的医生
更新:System.Data.Entity.DbFunctions.AddMinutes()
方法似乎出现错误
更新2:更新查询以包括System.Data.Entity.DbFunctions.DiffMinutes()
var filteredDoctors = await _context.Doctors
.Where(d => d.Specializare == request.Speciality)
.Include(d => d.Appointments)
.Where(x => !x.Appointments.Any(a => (a.Date != null && a.Date.Value.Date == date.Date) &&
(a.Date != null && date >= a.Date.Value) &&
(a.Date != null && a.Durata != null) &&
date <= System.Data.Entity.DbFunctions.AddMinutes(
a.Date.Value, System.Data.Entity.DbFunctions.DiffMinutes(TimeSpan.Zero, a.Durata.Value))))
.ToListAsync();
好的,基于Durata在约会期间是一个时间跨度,以下应该有效:
date <= DbFunctions.AddMinutes(a.Date.Value,
DbFunctions.DiffMinutes(TimeSpan.Zero, a.Durata.Value))