使用多个条件过滤子对象

时间:2016-02-11 10:53:31

标签: c# asp.net-mvc entity-framework linq

我在使用linq返回过滤记录时出现问题。涉及的对象如下所示:

预约

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; }
}

回调

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; }
}

用户

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; }
}

我试图返回符合以下条件的记录:

  • 约会start日期必须等于searchDate
  • 约会未删除
  • 约会start日期不得与用户已有的任何约会相冲突

我已尝试使用以下查询,但未返回任何结果(日期01/03/2016dd/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();
}

有人可以根据上述标准帮助我正确过滤吗?

修改

尝试澄清模型:

  • 用户有回调
    • 回调有预约

此查询的目的是搜索searchDate上用户尚未安排约会时间回调的所有约会。

3 个答案:

答案 0 :(得分:1)

我认为您需要对Any声明进行否定比较:

!a.Callbacks.Any(c =>
    (c.Appointment.Start == a.Start
    && c.AssignedTo.Ref == userName
    && c.Deleted == null)

因此,您只能从Callbacks获得a.CallbacksStart日期的Select(a)

此外,您可以在结尾处省略ToList - 语句,并立即致电SET NOCOUNT ON; DECLARE @user_name SYSNAME , @login_name SYSNAME; SELECT @user_name = 'HelpdeskUser', @login_name = 'Helpdesk' SELECT 'USE ' + QUOTENAME(NAME) + '; CREATE USER ' + QUOTENAME(@user_name) + ' FOR LOGIN ' + QUOTENAME(@login_name) + ' WITH DEFAULT_SCHEMA=[dbo]; EXEC sys.sp_addrolemember ''db_datareader'',''' + @user_name + '''; EXEC sys.sp_addrolemember ''db_denydatawriter'', ''' + @user_name + '''; GO' FROM sys.databases WHERE database_id > 4 AND state_desc = 'ONLINE'

答案 1 :(得分:1)

模特和你想要达到的目标对我来说并不是很清楚,但我会在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();

答案 2 :(得分:0)

试试这个,让我知道你得到的回报......

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();

这应该返回与searchDate参数

冲突的任何约会