日期比较并显示从控制器到视图的唯一日期列表

时间:2017-11-23 07:09:00

标签: c# asp.net-mvc linq

我有一个日期列表,我想与另一组日期(我使用JS)进行比较。

鉴于范围;

Start Date: 11/1/2017 12:00:00 AM
End Date: 11/20/2017 12:00:00 AM 

使用查询;

var mod = (from m in lst
                              where m.RId == EN &&  m.Date >= startDate && m.Date <= endDate
                              select m).Distinct();
                   var earlyMod = mod
                    .GroupBy(dt => dt.Date)
                    .Select(z => z.OrderBy(y => y.Date).First())
                    .ToList();
                    var presRowCount = 0; //to get the count


                    presRowCount = earlyMod.Count();
                    ViewBag.presRowCount = presRowCount;
                        return View(earlyMod);

在视图中;

 @foreach (var item in Model)
    {
<td>
                @Html.DisplayFor(modelItem => item.RegisterationId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DateTime)
                @ViewBag.Dates
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Date)
            </td> 
....}

我在视图中得到的是(我们称之为presentDates);

11/9/2017 8:02:09 AM    
11/10/2017 11:23:04 AM  
11/14/2017 10:11:11 AM

我在视图中也有这个JS函数,它有另一组日期(不包括周末);

<script>
var startDate = '@ViewBag.StartDate';
    var endDate = '@ViewBag.EndDate';

    startDate = new Date(startDate);
    endDate = new Date(endDate);
var weekDay = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    while (startDate <= endDate) {

        var new_weekDay = startDate.getDay();

        if (new_weekDay < 6 && new_weekDay > 0) {

            var month = startDate.getMonth() + 1;

            if (month <= 9) { month = "0" + month; }
            var day = startDate.getDate();
            if (day <= 9) { day = "0" + day; }
            document.write(day + "/" + month + "/" + startDate.getFullYear() + " (" + weekDay[new_weekDay] + ")<br />");
            console.log(day + "/" + month + "/" + startDate.getFullYear() + " (" + weekDay[new_weekDay] + ")<br />");
        }
        startDate.setDate(startDate.getDate() + 1)

我从上面的JS函数中获得了什么(我们称之为weekDates);

01/11/2017 (Wednesday)
02/11/2017 (Thursday)
03/11/2017 (Friday)
06/11/2017 (Monday)
07/11/2017 (Tuesday)
08/11/2017 (Wednesday)
09/11/2017 (Thursday)
10/11/2017 (Friday)
13/11/2017 (Monday)
14/11/2017 (Tuesday)
15/11/2017 (Wednesday)

我想要的是一种比较两组日期并仅显示两组中不存在的日期的方法(让我们称之为absentDates = weekDates - presentDates);

01/11/2017 (Wednesday)
02/11/2017 (Thursday)
03/11/2017 (Friday)
06/11/2017 (Monday)
07/11/2017 (Tuesday)
08/11/2017 (Wednesday)
13/11/2017 (Monday)

注意:日期的格式也不同。

1 个答案:

答案 0 :(得分:0)

您的控制器负责将正确的数据发送到视图,因此您应该在控制器中构建“缺席”日期列表,而不是在javascript中。

startDateendDate之间建立工作日列表,然后使用linq .Except()子句排除当前查询中包含的日期

// Build list of weekdays
List<DateTime> weekDays = new List<DateTime>();
int days = (endDate - startDate).Days;
for(int i = 0; i < days; i++)
{
    DateTime date = startDate.AddDays(i);
    if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
    {
        continue;
    }
    weekDays.Add(date);
}
// Get the dates to be excluded
IEnumerable<DateTime> excluded = earlyMod.Select(x => x.Date);
// Get the absent dates
IEnumerable<DateTime> absentDates = weekDays.Except(excluded);

理想情况下,您应该使用包含当前查询返回的对象的属性的视图模型以及IEnumerable<DateTime> AbsentDates属性,并将其传递给视图,以便强类型化(并删除所有{ {1}}属性),但您可以使用

将其传递给视图
ViewBag

并在视图中使用ViewBag.AbsentDates = absentDates; 循环来显示它们。

请注意,如果您需要特定的显示格式,则可以使用foreach