使用“;Linq方法”;

本文关键字:方法 Linq 使用 | 更新日期: 2023-09-27 17:58:33

〔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();
    }

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

使用“;Linq方法”;

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

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