C# 从IQueryable类型的嵌套DTO访问子属性<;T>;
使用.NET Core 3.1和EF Core 3.1 考虑到这3个嵌套DTO:C# 从IQueryable类型的嵌套DTO访问子属性<;T>;,c#,linq,entity-framework-core,mapping,dto,C#,Linq,Entity Framework Core,Mapping,Dto,使用.NET Core 3.1和EF Core 3.1 考虑到这3个嵌套DTO: public class AdminImmoOrderDto { public IQueryable<AdminImmoOrderProductDto> OrderProducts { get; set; } public bool IsCompleted { get; set; } } public class AdminImmoOrderProductDto { publi
public class AdminImmoOrderDto
{
public IQueryable<AdminImmoOrderProductDto> OrderProducts { get; set; }
public bool IsCompleted { get; set; }
}
public class AdminImmoOrderProductDto
{
public IQueryable<AdminImmoOrderProductFileDto> OrderFiles { get; set; }
public bool IsCompleted { get; set; }
}
public class AdminImmoOrderProductFileDto
{
public string FileName { get; set; }
public string FileUrl { get; set; }
}
引发以下EF核心异常
.All(od => od.IsCompleted == True)' by 'NavigationExpandingExpressionVisitor' failed.
This may indicate either a bug or a limitation in EF Core.
See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.
而且,您可以看到链接指的是客户端与服务器的评估
此属性IsCompleted用于以后筛选我的订单。我尝试在我的过滤器中移动这段代码,但没有成功。我还尝试在映射DTO之前执行此逻辑,使用linq代码块而不是子DTO中的属性(因为在映射完成之前它不存在)
我还尝试在调用.All()之前通过调用.ToList()或.AsEnumerable()显式强制客户机求值
我的问题是:如何根据OrderProductDto的子Dto中的IsCompleted属性设置OrderDto的IsCompleted属性,而不展平顶级IQueryable
谢谢你
更新10/03
我已尝试将其移动到我的筛选器查询对象,如果我可以按顺序筛选的子对象,则不需要该属性。遗憾的是,错误仍然是一样的。请参阅下面的代码
public enum OrdersFilterBy
{
NoFilter = 0,
IsCompleted = 1,
}
public static class AdminImmoOrderListDtoFilter
{
public static IQueryable<AdminImmoOrderDto> FilterOrdersBy(this IQueryable<AdminImmoOrderDto> orders, OrdersFilterBy filterBy)
{
switch(filterBy)
{
case OrdersFilterBy.NoFilter:
return orders;
case OrdersFilterBy.IsCompleted:
return orders.Where(o => o.OrderProducts.AsEnumerable().All(op => op.IsCompleted == true));
default:
throw new ArgumentOutOfRangeException
(nameof(filterBy), filterBy, null);
}
}
}
}
无论是否使用.AsEnumerable()展开子集合,结果都是相同的。与前一个错误相同
更新10/03第2部分
下面是一个API结果的示例,如果它有助于更好地了解层次结构
{
"id": "1",
"orderPicture": null,
"saleType": 2,
"clientName": "Toto",
"clientPhone": "000000000",
"fullAddress": "Toto road, 420",
"smsCode": "XYZVDF",
"creationDate": "2020-03-09T15:08:36.157",
"visitDate": "2020-03-16T08:00:00",
"orderProducts": [
{
"productTypeID": 1,
"productName": "A sample product",
"visitDate": "2020-03-16T08:00:00",
"urgenceDate": null,
"description": "7",
"companyEmployeeNames": null,
"isCompleted": false,
"orderFiles": []
}
]
},
.All(od => od.IsCompleted == True)' by 'NavigationExpandingExpressionVisitor' failed.
This may indicate either a bug or a limitation in EF Core.
See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.
public enum OrdersFilterBy
{
NoFilter = 0,
IsCompleted = 1,
}
public static class AdminImmoOrderListDtoFilter
{
public static IQueryable<AdminImmoOrderDto> FilterOrdersBy(this IQueryable<AdminImmoOrderDto> orders, OrdersFilterBy filterBy)
{
switch(filterBy)
{
case OrdersFilterBy.NoFilter:
return orders;
case OrdersFilterBy.IsCompleted:
return orders.Where(o => o.OrderProducts.AsEnumerable().All(op => op.IsCompleted == true));
default:
throw new ArgumentOutOfRangeException
(nameof(filterBy), filterBy, null);
}
}
}
}
return orders.Where(o => o.OrderProducts.AsEnumerable().All(op => op.IsCompleted == true));
{
"id": "1",
"orderPicture": null,
"saleType": 2,
"clientName": "Toto",
"clientPhone": "000000000",
"fullAddress": "Toto road, 420",
"smsCode": "XYZVDF",
"creationDate": "2020-03-09T15:08:36.157",
"visitDate": "2020-03-16T08:00:00",
"orderProducts": [
{
"productTypeID": 1,
"productName": "A sample product",
"visitDate": "2020-03-16T08:00:00",
"urgenceDate": null,
"description": "7",
"companyEmployeeNames": null,
"isCompleted": false,
"orderFiles": []
}
]
},