【问题标题】:MVC5 LINQ display only if latest record is a certain condition仅当最新记录是特定条件时才显示 MVC5 LINQ
【发布时间】:2016-06-04 13:47:33
【问题描述】:

请原谅标题不清楚,想不出更好的表达方式。

我有 2 个表,用户和上诉。

这个过程是这样运作的:

  1. 用户可以请求查看其他用户的信息。后 请求,请求将被插入到 Appeal 表中 isApproved 列默认设置为 0,即 PendingApproved 为 1,Rejected 为 2。用户提出请求后,他将无法将该用户视为用户列表的一部分他可以要求谁查看信息。
  2. 如果请求已被接受或仍处于待处理状态,则它不应显示在他可以请求查看信息的用户列表中。 但是,如果它被拒绝,它将在列表中重新显示,以便用户可以以某种方式再次“重新请求”它。该条件应仅基于发出的最新请求,而不是较早的请求。

以下是我迄今为止想出的声明,我正在努力实现上述目标。

(from user in db.Users
  where user.Roles.Any(r => r.RoleId == roleId)
  where !db.Appeal.Any(y => y.PatientId == user.Id) // if no record found in appeal
  || // or if there's a record
  (db.Appeal.OrderByDescending(a => a.Time).FirstOrDefault().PatientId == user.Id
  && db.Appeal.Any(a => a.PatientId == user.Id && a.IsApprove == 2))
  // but only if the latest request is rejected, show back up 
  // in the user list again so it can be requested again

     select new Patient {
       PatientId = user.Id,
       PatientName = user.FullName,
       UniqueId = user.UniqueId
     }).OrderBy(x => x.PatientId);

【问题讨论】:

    标签: c# sql linq asp.net-mvc-5


    【解决方案1】:

    终于解决了问题。

    nameQuery = (from user in db.Users
      where user.Roles.Any(r => r.RoleId == roleId)
      where !db.Appeal.Any(y => y.PatientId == user.Id) // if no record found in appeal
      || 
      db.Appeal.OrderByDescending(x => x.Time).FirstOrDefault(p => p.PatientId 
      == user.Id).IsApprove == 2
      // or got appeal, but the latest result is rejected
    
      select new Patient {
       PatientId = user.Id,
       PatientName = user.FullName,
       UniqueId = user.UniqueId
       }).OrderBy(x => x.PatientId);
    

    改变

    (db.Appeal.OrderByDescending(a => a.Time).FirstOrDefault().PatientId == user.Id && db.Appeal.Any(a => a.PatientId == user.Id && a.IsApprove == 2))

    db.Appeal.OrderByDescending(x => x.Time).FirstOrDefault(p => p.PatientId == user.Id).IsApprove == 2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多