1

我想获取所有每月过期的产品,这是查询:

_customerProductsRepository
    .Where(
        d =>
        !d.ReleaseDate.HasValue &&
        EntityFunctions.AddMonths(d.RenewalDate ?? d.AcquireDate, 1) < now)
    .ToArray();

AcquireDate是产品的第一次购买,RenewalDate是产品的最后一次续订。

由于某种原因,它转换为以下 SQL:

SELECT
[Extent1].[CustomerDidId] AS [CustomerDidId],
[Extent1].[DidNumber] AS [DidNumber],
[Extent1].[CountryId] AS [CountryId],
[Extent1].[CustomerId] AS [CustomerId],
[Extent1].[AcquireDate] AS [AcquireDate],
[Extent1].[ReleaseDate] AS [ReleaseDate],
[Extent1].[RenewalDate] AS [RenewalDate],
[Extent1].[RenewalNotificationDate] AS [RenewalNotificationDate]
FROM [dbo].[CustomerDids] AS [Extent1]
WHERE ([Extent1].[ReleaseDate] IS NULL) AND ((DATEADD (month, 1, [Extent1].[Rene
walDate])) < @p__linq__0)

应该有一个案例陈述提到'??' 而是签名 - 它完全删除了 AcquireDate 列。

我怎样才能在它周围走来走去?

4

3 回答 3

1

您已根据需要配置了该属性RenewalDate。因此,EF 将通过评估“if”条件的结果来优化查询。

于 2011-12-31T00:28:09.937 回答
0

您是否尝试过这样做:

_customerProductsRepository
    .Where(
        d =>
        !d.ReleaseDate.HasValue &&
        (d.RenewalDate.HasValue ? 
              EntityFunctions.AddMonths(d.RenewalDate.Value, 1) < now) :
              EntityFunctions.AddMonths(d.AcquireDate, 1) < now))
    .ToArray();
于 2011-12-30T08:39:38.690 回答
0

在一种情况下,如果您无法将其用作 IQueryable,请将所有数据作为 IEnumerable(调用asEnumerable),然后通过您指定的日期方法过滤掉。

于 2011-12-30T08:51:01.117 回答