使用“Linq方法”处理DateTime

时间:2011-06-06 14:54:48

标签: c# asp.net entity-framework

[ASP.NET 4.0 / EF 4.1]

您好,

我正在尝试使用“Linq方法”根据日期时间字段过滤数据源,但我收到错误:“此处仅支持基本类型(例如Int32,String和Guid')上下文中,”

我知道实体框架在处理日期时有一些限制,但我需要的是一些处理这个问题的优雅解决方案。

我的代码是:

    public IList<Order> GetOrders(int? orderId = null, string customerId = null, int? employeeId = null, DateTime? orderDateFrom = null, DateTime? orderDateUntil=null, DateTime? requiredDate = null, DateTime? shippedDate = null)
    {
        IQueryable<Order> result;

        result = from order in ctx.Orders.Include("Order_Details")
                 select order;

        // Apply filters to the base query
        if (orderId != null)
            result = result.Where(o => o.OrderID.Equals(orderId));

        if (!String.IsNullOrEmpty(customerId))
            result = result.Where(o => o.CustomerID.ToUpper().Equals(customerId.ToUpper()));

        if (employeeId != null)
            result = result.Where(o => o.EmployeeID.Equals(employeeId));

        if (orderDateFrom != null)
            result = result.Where(o => o.OrderDate >= orderDateFrom);

        if (orderDateUntil != null)
            result = result.Where(o => o.OrderDate <= orderDateUntil);

        if (requiredDate != null)
            result = result.Where(o => o.RequiredDate == requiredDate);

        if (shippedDate != null)
            result = result.Where(o => o.ShippedDate == shippedDate);

        return result.ToList();
    }

当代码执行查询(result.ToList())时,它会抛出异常。如果我删除datetime .Where子句,它可以正常工作。

谢谢!

解决方案

我已将代码更改为:

    public IList<Order> GetOrders(int? orderId = null, string customerId = null, int? employeeId = null, DateTime? orderDateFrom = null, DateTime? orderDateUntil=null, DateTime? requiredDate = null, DateTime? shippedDate = null)
    {
        IQueryable<Order> result;

        result = from order in ctx.Orders.Include("Order_Details")
                 select order;

        // Apply filters to the base query
        if (orderId != null)
            result = result.Where(o => o.OrderID.Equals(orderId.Value));

        if (!String.IsNullOrEmpty(customerId))
            result = result.Where(o => o.CustomerID.ToUpper() == customerId.ToUpper());

        if (employeeId != null)
            result = result.Where(o => o.EmployeeID == employeeId.Value);

        if (orderDateFrom != null)
            result = result.Where(o => o.OrderDate >= orderDateFrom.Value);

        if (orderDateUntil != null)
            result = result.Where(o => o.OrderDate <= orderDateUntil.Value);

        if (requiredDate != null)
            result = result.Where(o => o.RequiredDate == requiredDate.Value);

        if (shippedDate != null)
            result = result.Where(o => o.ShippedDate == shippedDate.Value);

        return result.ToList();
    }

如果有人有更好的解决方案,请告诉我。

1 个答案:

答案 0 :(得分:4)

您使用的是可以为空的DateTime?在你的例子中。我不确定你的数据库列是否允许NULL,但我会使用date.Value来传递参数的值,以确保EF不会落在它上面。请注意,对于可空类型,您可以使用HasValue属性来检查参数是否包含正确的值。这导致:

if (orderDateFrom.HasValue)
   result = result.Where(o => o.OrderDate >= orderDateFrom.Value);