1

考虑以下 SQL 查询:

SELECT
   *
FROM 
   Orders
WHERE
   (AdjShipDate IS NOT NULL OR EstShipDate IS NOT NULL)
    AND
    CASE 
        WHEN AdjShipDate IS NOT NULL THEN AdjShipDate
        WHEN AdjShipDate IS NULL AND EstShipDate IS NOT NULL THEN EstShipDate
    END
    BETWEEN '2011-07-01' AND '2011-07-30'

案例的 WHEN 表达式取决于我要查询的表中的值 (AdjShipDate)。我将如何在 Linq to SQL 中编写类似的查询?

4

2 回答 2

1

它本质上是一个 COALESCE:

where (o.AdjShipDate ?? o.EstShipDate) >= new DateTime(2011,07,01) 
   && (o.AdjShipDate ?? o.EstShipDate) <= new DateTime(2011,07,30)

虽然我不确定??Linq-to-SQL 支持运算符。如果不是你可以试试

where (o.AdjShipDate != NULL 
           && o.AdjShipDate >= new DateTime(2011,07,01) 
           && o.AdjShipDate <= new DateTime(2011,07,30))
   || (o.AdjShipDate == NULL && o.EstShipDate != NULL 
           && o.EstShipDate >= new DateTime(2011,07,01) 
           && o.EstShipDate <= new DateTime(2011,07,30))
于 2012-11-02T22:01:38.127 回答
0

它绝对等同于:

SELECT
   *
FROM 
   Orders
WHERE
    (AdjShipDate IS NOT NULL and AdjShipDate BETWEEN '2011-07-01' AND '2011-07-30')
     OR
    (AdjShipDate IS NULL and EstShipDate IS NOT NULL ANd EstShipDate BETWEEN '2011-07-01' AND '2011-07-30') 

只需将其编译为 LinqToSql。

于 2012-11-02T22:06:54.880 回答