LINQ:获取不在记录中的项目 - C#

LINQ: Get an item that is not in the records - C#

这是我

的扩展问题
class EmployeeSchedule
{
   public int Id { get; set; }
   public string Name { get; set; }
   public DateTime WorkDate { get; set; }
   public bool isOff { get; set; }
}

现在在这种情况下,我想从列表中找到 Id,其 WorkDate 未在 4/12/2016 中找到作为我的标准。

List<Staff> workers = new List<Staff>()
{
    new Staff { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
    new Staff { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
    new Staff { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/13/2016"), IsOff = false},
    new Staff { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/14/2016"), IsOff = false},
    new Staff { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/15/2016"), IsOff = false},
    new Staff { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/16/2016"), IsOff = false},
    new Staff { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/17/2016"), IsOff = false},
    new Staff { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
    // new Staff { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
    new Staff { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/13/2016"), IsOff = false},
    new Staff { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/14/2016"), IsOff = false},
    new Staff { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/15/2016"), IsOff = false},
    new Staff { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/16/2016"), IsOff = false},
    new Staff { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/17/2016"), IsOff = false},
};

到目前为止我已经试过这个代码:

var notInDate = workers.Where(x => !workers.Any(y => Convert.ToDateTime("4/12/2016") != x.WorkDate));

foreach (var item in notInDate)
{
    Console.WriteLine(item.Id + " " + item.WorkDate.Date);
}

输出为:ID :1 WorkDate: 4/12/2016 12:00:00 AM

而这个具有以下输出:

var notInDate = workers.Where(x => workers.Any(y => Convert.ToDateTime("4/12/2016") != x.WorkDate));

ID :1 WorkDate: 4/11/2016 12:00:00 AM
ID :1 WorkDate: 4/13/2016 12:00:00 AM
ID :1 WorkDate: 4/14/2016 12:00:00 AM
ID :1 WorkDate: 4/15/2016 12:00:00 AM
ID :1 WorkDate: 4/16/2016 12:00:00 AM
ID :1 WorkDate: 4/17/2016 12:00:00 AM
ID :2 WorkDate: 4/11/2016 12:00:00 AM
ID :2 WorkDate: 4/13/2016 12:00:00 AM
ID :2 WorkDate: 4/14/2016 12:00:00 AM
ID :2 WorkDate: 4/15/2016 12:00:00 AM
ID :2 WorkDate: 4/16/2016 12:00:00 AM
ID :2 WorkDate: 4/17/2016 12:00:00 AM

第二个例子只是删除了WorkDate is 4/12/2016

的记录

我想得到谁的 Id = 2 因为 Id 没有 4/12/2016

WorkDate

您可以这样做,使用 GroupById 上分组,然后使用 Any 扩展验证员工是否在该日期工作..

var notInDate = workers.GroupBy(g=>g.Id)
                       .Where(x => !x.Any(y => DateTime.ParseExact("4/12/2016","M/d/yyyy", null) == y.WorkDate))
                       .FirstOrDefault();  // Get the first matching staff.
if(notInDate != null)
{
    id = notInDate.Key; // StaffId
}   

如果您想要当天没有工作的人的所有条目,您需要按 id 分组(以获取每个人的组)并过滤不包含日期的个别组。像;

var notInDate = 
         // Create a group per employee
  workers.GroupBy (x => x.Id)
         // Keep only groups that didn't work
         .Where (x => !x.Any (y => y.WorkDate == Convert.ToDateTime ("4/12/2016")))
         // Get only the key for each group
         .Select (x => x.Key);