如何将EF List对象转换为dateTime类型的arrayList

时间:2017-07-25 11:34:49

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 entity-framework-4 entity-framework-6

我正在休假申请。我需要获取排除的日期并将其转换为dateTime数组。

我使用以下方法来部分实现此目的。我创建了一个Helper文件夹,然后将以下类添加到该文件夹​​中:

@Profile("production")

我像下面那样实例化了类,并将namespace eLeave.Helpers { public class BusinessDaysCalculator { public int Calculate(DateTime startDate, DateTime endDate, DateTime[] holidays) { startDate = FixStartDate(startDate); endDate = FixEndDate(endDate.Date); if (startDate > endDate) return 0; else return CalculateDifference(startDate, endDate, holidays); } private int CalculateDifference(DateTime startDate, DateTime endDate, DateTime[] holidays) { int difference = (int)(endDate - startDate).TotalDays; int weeks = difference / 7; if (endDate.DayOfWeek < startDate.DayOfWeek) weeks++; difference -= weeks * 2; difference -= HolidayCount(startDate, endDate, holidays); return difference + 1; } private int HolidayCount(DateTime startDate, DateTime endDate, DateTime[] holidays) { int holidayCount = 0; foreach (DateTime holiday in holidays) { if (HolidayInRange(startDate, endDate, holiday)) holidayCount++; } return holidayCount; } private DateTime FixStartDate(DateTime date) { if (date.DayOfWeek == DayOfWeek.Sunday) date = date.AddDays(1); else if (date.DayOfWeek == DayOfWeek.Saturday) date = date.AddDays(2); return date.Date; } private bool HolidayInRange(DateTime startDate, DateTime endDate, DateTime holiday) { return (holiday >= startDate && holiday <= endDate && holiday.DayOfWeek != DayOfWeek.Saturday && holiday.DayOfWeek != DayOfWeek.Sunday); } private DateTime FixEndDate(DateTime date) { if (date.DayOfWeek == DayOfWeek.Sunday) date = date.AddDays(-2); else if (date.DayOfWeek == DayOfWeek.Saturday) date = date.AddDays(-1); return date.Date; } } } 传递给它,如下所示:

excludedDate

该实现意味着工作但是出错了但是在尝试使用// Get the Excluded dates var excludedDates = _context.ExcludedDate.Where(d=>d.ExcludedDates >= StartDate).Where(d=>d.ExcludedDates <= EndDate).ToArray(); BusinessDaysCalculator calc = new BusinessDaysCalculator(); int days = calc.Calculate(StartDate, EndDate, excludedDates); DateTime Arrray时

holidays

我在上面的 DateTime[] holidays = new DateTime[3]; holidays[0] = new DateTime(2017, 08, 15); holidays[1] = new DateTime(2017, 09, 1); 数组

中获得了排除日期

我需要获取excludedDates的值,并将其与我的类一起使用以使其工作。

我在下面得到的错误:

holiday

排除日期的模型如下:

Error   1   The best overloaded method match for 'eLeave.Helpers.BusinessDaysCalculator.Calculate(System.DateTime, System.DateTime, System.DateTime[])' has some invalid arguments  C:\Apps\WebApplications\eLeave\eLeave\eLeave\Controllers\RequestController.cs   100 32  eLeave


Error   2   Argument 3: cannot convert from 'eLeave.Models.ExcludedDate[]' to 'System.DateTime[]'   C:\Apps\WebApplications\eLeave\eLeave\eLeave\Controllers\RequestController.cs   100 67  eLeave

1 个答案:

答案 0 :(得分:1)

您的查询返回的是ExcludedDate类的数组,而不是类型DateTime - 您需要.Select()子句来仅选择对象的ExcludedDates属性。

将查询更改为

DateTime[] excludedDates = _context.ExcludedDate
    .Where(d=>d.ExcludedDates >= StartDate && d.ExcludedDates <= EndDate)
    .Select(x => x.ExcludedDates)
    .ToArray();

请注意,不清楚为什么你的属性可以为空(这个属性的值不会有null,而且这个名称与表名的关系有点混乱。我建议你把它改成public DateTime Date { get; set; }