Linq - 包含 DateTime 字段的动态查询

Linq - Dynamic query that contain DateTime filed

我搜索如下图:

我通过以下代码创建查询:

StringBuilder query = new StringBuilder();

string OrderBy = null;
switch (sortOrder)
{
    case "orderCode_desc":
        OrderBy = " OrderCode desc";
        break;
    case "mobile":
        OrderBy = "Mobile";
        break;
    case "mobile_desc":
        OrderBy = "Mobile desc";
        break;
    case "date":
        OrderBy = "OrderDate";
        break;
    case "date_desc":
        OrderBy = "OrderDate desc";
        break;
    case "orderCode":
    default:
        OrderBy = "OrderCode";
        break;
}

if (filters.SelectedProducts.NotNullString())
{
    var SelectedProducts = filters.SelectedProducts.Split(',');
    query.Append("(");
    for (int i = 0; i < SelectedProducts.Length; i++)
    {
        query.AppendFormat("uag.oag.ordDet.ProductID == {0}" , SelectedProducts[i]);
        if (i != SelectedProducts.Length - 1)
        {
            query.Append(" || ");
        }
    }
    query.Append(") && ");
}


if (filters.DateFrom != null && filters.DateTo != null)
{
    query.AppendFormat("(uag.oag._order.order.OrderDate >= {0} && uag.oag._order.order.OrderDate <= {1} )", filters.DateFrom.Date, filters.DateTo.Date);
}
if (filters.DateFrom != null && filters.DateTo == null)
{
    query.AppendFormat("( uag.oag._order.order.OrderDate >= {0} )", filters.DateFrom.Date);
}
if (filters.DateFrom == null && filters.DateTo != null)
{
    query.AppendFormat("( uag.oag._order.order.OrderDate <= {0} )", filters.DateTo.Date);
}

string _query = query.ToString();
_query = _query.EndsWith("&& ") ? _query.Remove(_query.Length - 3) : _query;

orders = db.orders
        .Join(db.order_statuses, order => order.OrderStatusID, OrderStatus => OrderStatus.OrderStatusID, (order, OrderStatus) => new { order, OrderStatus })
        .Join(db.order_details, _order => _order.order.OrderID, ordDet => ordDet.OrderID, (_order, ordDet) => new { _order, ordDet })
        .Join(db.agents_info, oag => oag._order.order.AgentID, ag => ag.AgentInfoID, (oag, ag) => new { oag, ag })
        .Join(db.users, uag => uag.ag.UserID, u => u.UserID, (uag, u) => new { uag, u })
        .AsQueryable()
        .Where(_query)
        .Select(m => new OrderList
        {
            OrderID = m.uag.oag._order.order.OrderID,
            OrderCode = m.uag.oag._order.order.OrderCode,
            OrderDate = m.uag.oag._order.order.OrderDate,
            OrderStatus = m.uag.oag._order.OrderStatus.OrderStatusTitle,
            TotalPrice = m.uag.oag._order.order.TotalPrice,
            Mobile = m.u.Mobile
        }).OrderBy(OrderBy).Skip(PageSize * (page - 1)).Take(PageSize).ToList();

我收到一个错误:

Operator '>=' incompatible with operand types 'DateTime' and 'Int32'

here也是同样的问题,他的问题用参数化查询解决了。我使用了参数化查询,但问题没有解决。 我该如何解决这个问题?

根据 documentation,您需要使用 public 构造函数来创建 DateTime 对象,例如

query.AppendFormat("(uag.oag._order.order.OrderDate >= {0} && uag.oag._order.order.OrderDate <= {1})",
    filters.DateFrom.Date.ToString(@"Da\teTi\me(yyyy,M,d)"),
    filters.DateTo.Date.ToString(@"Da\teTi\me(yyyy,M,d)"));