[英]Filtering Child Object with Multiple Criteria
I'm having an issue filtering records returned using linq. 我在过滤使用linq返回的记录时遇到问题。 The objects involved look like this:
涉及的对象如下所示:
Appointment 约定
public partial class Appointment
{
public Appointment()
{
Callbacks = new HashSet<Callback>();
}
[Key()]
public int AppointmentId { get; set; }
public DateTime Start { get; set; }
public DateTime? Deleted { get; set;}
public virtual ICollection<Callback> Callbacks { get; set; }
}
Callback 打回来
public partial class Callback
{
[Key()]
public int CallbackId { get; set; }
public DateTime? Deleted { get; set; }
public virtual Appointment Appointment { get; set; }
public virtual User AssignedTo { get; set; }
}
User 用户
public partial class User
{
public User()
{
Callbacks = new HashSet<Callback>();
}
[Key()]
public int UserId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Ref { get; set; }
public virtual ICollection<Callback> Callbacks { get; set; }
}
I'm trying to return records that meet the following criteria: 我正在尝试返回符合以下条件的记录:
start
date must equal searchDate
start
日期必须等于searchDate
start
date must not clash with any appointments that the user already has start
日期不得与用户已经拥有的任何约会冲突 I've tried using the following query, but no results are returned (there are appointments available for the date 01/03/2016
( dd/mm/yyyy
). 我尝试使用以下查询,但未返回任何结果(有一些约会可用于日期
01/03/2016
( dd/mm/yyyy
)。
public List<AppointmentSearchResultsViewModel> SearchByDate(DateTime searchDate, string userName)
{
return _context.Appointments
.Where(a =>
a.Start.Date == searchDate
&& a.Deleted == null
&& a.Callbacks.Any(c =>
!(c.Appointment.Start != a.Start
&& c.AssignedTo.Ref == userName
&& c.Deleted == null)
))
.OrderBy(a => a.Start)
.Select(a)
.ToList();
}
Could anyone help me with how to filter correctly based on the criteria above? 有人可以帮助我如何根据上述条件正确进行过滤吗?
Edit 编辑
To try and clarify the model: 尝试阐明模型:
The aim of this query is to search for all appointments on the searchDate
where the user does not already have a callback scheduled for the appointment time. 该查询的目的是在
searchDate
上搜索所有约会,在该searchDate
中用户尚未为约会时间安排回searchDate
。
I think you need a negative comparison for your Any
-statement: 我认为您需要对
Any
语句进行否定比较:
!a.Callbacks.Any(c =>
(c.Appointment.Start == a.Start
&& c.AssignedTo.Ref == userName
&& c.Deleted == null)
Thus you only got those Callbacks
from a.Callbacks
which have a different Start
-date. 因此,您仅从
a.Callbacks
获得了具有不同Start
日期的Callbacks
。
Furtheremore you can ommit the Select(a)
-statement at the end and immediately call ToList
. 此外,您可以在最后省略
Select(a)
语句,然后立即调用ToList
。
The model and what you are trying to achieve is not really clear to me, but I will try my chances anyway on the part O could understand: 该模型和您要实现的目标对我来说还不是很清楚,但是无论如何,我都会尽我所能尝试O可以理解的部分:
return _context.Appointments
.Where(a =>
a.Start.Date == searchDate
&& a.Deleted == null
&& !a.Callbacks.Any(c =>
(c.Appointment.Start == a.Start
&& c.AssignedTo.Ref == userName
&& c.Deleted == null)
))
.OrderBy(a => a.Start)
.ToList();
Try this and let me know what you get in return... 试试这个,让我知道你得到的回报...
return context.Users.Where(user => user.Ref = userName)
.SelectMany(user => user.Callbacks)
.Where(cb => cb.Appointment.Deleted == null)
.Where(cb => cb.Appointment.Start == searchDate)
.Select(cb => cb.Appointment)
.ToList();
This should return any appointments that clash with the searchDate
parameter 这应该返回任何与
searchDate
参数冲突的searchDate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.