比较c#中的两个日期

时间:2016-03-02 12:23:51

标签: c# asp.net .net datetime linq-to-entities

控制器:

Identity(1,1) - it means start from 1 and increment it by 1 on new insert

型号:

public JsonResult TestArray(string id)
{
    //warehouse_order warehouse_order = new warehouse_order();
    int[] finishedOrders = new int[8];
    int countErledigt;

    var stichtag = DateTime.Today;
    for (int i = 0; i < 8; i++)
    {
        stichtag = DateTime.Today.AddDays(-i);
        countErledigt = db.warehouse_order.Where(c => c.end_time == stichtag ).Count();
        finishedOrders[i] = countErledigt;
    }

    int[] test = finishedOrders;

    return Json(test, JsonRequestBehavior.AllowGet);
}

我已经能够比较控制器中的两个日期了。     任何想法我怎么能这样做?     非常感谢

1 个答案:

答案 0 :(得分:0)

由于end_time属性的类型为Nullable<System.DateTimeOffset>,但stichtag的类型为DateTime,因此您无法直接比较它们。

您应该从DateTime中提取end_time部分或将stichtag包裹到DateTimeOffset

由于在LINQ to Entities中,您无法使用未映射到Where表达式中的数据库列的属性 - 您唯一的选择是第二个:

var stichtag = new DateTimeOffset(DateTime.Today.AddDays(-i));
countErledigt = db.warehouse_order.Where(c => c.end_time == stichtag).Count();