使用 EF Core 6 Cosmos DB 在谓词内部使用实体内部列表的计数进行查询的问题

问题描述 投票:0回答:0

考虑到这个简单的模型,我想使用 EF Core 6 Cosmos 恢复拥有 4 到 6 个订单的客户。

public class CustomerEntity { 
    public string Id { get; set; } 
    public string Name{ get; set; } 
    public IList<Order> Orders { get; set; } 
}

public class Order: 
{
    public string Id { get; set; }
    public double TotalPrice { get; set; }
    public double TotalDiscount { get; set; }
}

对于这种情况,我尝试使用 DbContext 执行下一个 Linq 表达式:

dbContext.Customers.Where(c => c.Orders.Count >= 4 && c.Orders.Count <= 6).Count()

这种情况导致我的应用程序出现异常:

Executed 'Function' (Failed, Id=69ba3785-03d2-4605-b94b-3ab6017b5d78, Duration=261ms) [2023-03-31T08:25:24.418Z] System.Private.CoreLib: Exception while executing function: GetStreams. Microsoft.EntityFrameworkCore: The LINQ expression 'DbSet<CustomerEntity>() [2023-03-31T08:25:24.420Z]     .Where(EF.Property<IList<Order>>(s, "Order") [2023-03-31T08:25:24.421Z]         .AsQueryable() [2023-03-31T08:25:24.423Z]         .Count() >= __get_Item_0 && EF.Property<IList<Order>>(s, "Order") [2023-03-31T08:25:24.424Z]         .AsQueryable() [2023-03-31T08:25:24.424Z]         .Count() <= __get_Item_1 && True && True && True)' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

我尝试了另一种解决方案,比如使用 SELECT,WHERE 按此顺序再次应用 Count。

我也看到存在新版本的 EF Core Cosmos 7,但我认为我不能在 Azure Functions 中使用它,因为两者之间不兼容。

entity-framework-core azure-functions azure-cosmosdb entity-framework-core-cosmosdb
© www.soinside.com 2019 - 2024. All rights reserved.