我一直在尝试从datetime list
排除//LATES APPROVED LIST
var lateReq = (from mk in db.Late
where mk.EmpID == EmployeeRec.EmpId
&& mk.TeamLeadAppr == 1 && mk.DeptLeadAppr == 1
select mk).ToList();
List<DateTime> latesApproved = new List<DateTime>();
List<LateRequest> lateRequest = new List<LateRequest>();
foreach (var gk in lateReq)
{
latesApproved.Add(gk.lateDate.Value);
}
//here lets say there is a date in latesApproved i.e. 13/3/2018 12:00:00 AM
//now in the `lateComings` list there are datetimes i.e. 13/3/2018 10:26:35 AM
//exclude the lates approved list from the lateComings list
List<DateTime> LateComingList = lateComings.ToList();
,情景是这样;
lateComings
//排除上面的列表不排除它,因为它无法在 List<DateTime> lateComingsWithoutApproved = LateComingList.Except(latesApproved).ToList();
lateComings = lateComingsWithoutApproved;
//list of time differences
var timeDiff = new List<TimeSpan>();
foreach (DateTime ts in lateComings)
{
if (ts.TimeOfDay > AT)//TimeSpan.FromHours(AT)
{
timeDiff.Add(ts.TimeOfDay - AT);//TimeSpan.FromHours(AT));
}
}
countLates = lateComings.Count();
//fetching the minutes from the above list which is in the format of 02:43:00
var minutesList = timeDiff.Select(t => Convert.ToInt32(t.TotalMinutes)).ToList();
//我需要带有datetime的最终列表,因为我已经进一步使用了列表的时间部分,如下所示;
latesApproved list
水晶照片;
lateComings list
01/03/2018 12:00:00:AM
05/03/2018 12:00:00:AM
09/03/2018 12:00:00:AM
13/03/2018 12:00:00:AM
{{1}}
05/03/2018 11:00:32:AM
06/03/2018 10:54:33:AM
07/03/2018 08:34:47:AM
08/03/2018 12:30:40:AM
09/03/2018 10:03:00:AM
10/03/2018 11:03:00:AM
11/03/2018 11:30:40:AM
12/03/2018 10:30:40:AM
13/03/2018 08:30:00:AM
一些最终列表应该是:
06/03/2018 10:54:33:AM
07/03/2018 08:34:47:AM
08/03/2018 12:30:40:AM
10/03/2018 11:03:00:AM
11/03/2018 11:30:40:AM
12/03/2018 10:30:40:AM
答案 0 :(得分:1)
这里遇到的麻烦是,当你运行Except
时,它会查看实际的日期时间值并考虑时间,因此它与其他任何内容都不匹配。您需要使用带有相等比较器的重载,以确保它们在日期级别处理相同。请考虑以下事项:
// class Program : IEqualityComparer<DateTime> // place something like this at the class definition.
bool IEqualityComparer<DateTime>.Equals(DateTime x, DateTime y)
{
return x.Date == y.Date; // Compare the dates and not the times.
}
int IEqualityComparer<DateTime>.GetHashcode(DateTime obj)
{
return obj.Date.GetHashCode();
}
在我的例子中,我使用了我编写此代码的类作为比较器。作为一个控制台应用程序,它是Program类,所以我的调用如下
var lateComingsWithoutApproved = LateComingList.Except(latesApproved, new Program()).ToList();
您需要将其替换为您使用的类的实例。
<强>更新强> 为了实现这一点,您必须实现一个类(通常是需要进行比较的类)。假设您的类名为LateArrivalController,则更改定义...
public class LateArrivalController : Controller, IEqualityComparer<DateTime>
然后以这种方式传递......
var lateComingsWithoutApproved = LateComingList.Except(latesApproved, this).ToList();
答案 1 :(得分:0)
这看起来像是为我做的工作:
List<DateTime> latesApproved = new List<DateTime>
{
Convert.ToDateTime("01/03/2018 12:00:00"),
Convert.ToDateTime("05/03/2018 12:00:00"),
Convert.ToDateTime("09/03/2018 12:00:00"),
Convert.ToDateTime("13/03/2018 12:00:00")
};
List<DateTime> lateComings = new List<DateTime>
{
Convert.ToDateTime("05/03/2018 11:00:32"),
Convert.ToDateTime("06/03/2018 10:54:33"),
Convert.ToDateTime("07/03/2018 08:34:47"),
Convert.ToDateTime("08/03/2018 12:30:40"),
Convert.ToDateTime("09/03/2018 10:03:00"),
Convert.ToDateTime("10/03/2018 11:03:00"),
Convert.ToDateTime("11/03/2018 11:30:40"),
Convert.ToDateTime("12/03/2018 10:30:40"),
Convert.ToDateTime("13/03/2018 08:30:00")
};
List<DateTime> dateTimes = lateComings
.Where(x => !latesApproved
.Select(a => a.Date)
.Contains(x.Date))
.ToList();