linq查询无法翻译。要么以可翻译的形式重写查询。NETCore

时间:2020-05-09 11:11:44

标签: entity-framework linq .net-core

我有一个查询,您可以在这里看到:

 public Domain.PetrolReceptions.PetrolReception GetLastOpenNotCanceledByVIN(string vin,
            double openPetrolTestTimeInDays)
            => _dbContext.PetrolReceptions.Where(g =>
                g.Car.carBody.vin == vin && g.CreatedDate.AddDays(openPetrolTestTimeInDays) >= DateTime.Today && g.Status != Domain.PetrolReceptions.PetrolReception.PetrolReceptionStatus.Canceled_Passed && g.Status != Domain.PetrolReceptions.PetrolReception.PetrolReceptionStatus.Canceled_Failed && g.Status != Domain.PetrolReceptions.PetrolReception.PetrolReceptionStatus.Canceled_Pending).OrderBy(g => g.CreatedDate).LastOrDefault();

当我想通过运行视觉程序执行此代码时,出现此错误:

The LINQ expression 'DbSet<PetrolReception>
    .Where(p => p.Car.carBody.vin == __vin_0 && p.CreatedDate.AddDays(__openPetrolTestTimeInDays_1) >= DateTime.Today && (int)p.Status != 5 && (int)p.Status != 4 && (int)p.Status != 3)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

1 个答案:

答案 0 :(得分:0)

将AddDays应用于参数,而不是列表达式。

更改

g.CreatedDate.AddDays(openPetrolTestTimeInDays) >= DateTime.Today 

var endDate = DateTime.Today.AddDays(-1 * openPetrolTestTimeInDays);
. . .
g.CreatedDate <= endDate