考虑以下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中编写类似的查询到SQL?
答案 0 :(得分: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))
答案 1 :(得分: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。