【发布时间】:2021-08-02 05:30:32
【问题描述】:
使用 Asp.Net 3.1 Core EntityFramework Core LINQ,假设我有一个 Order 表和一个 Customer 表:
public class Order
{
public long Id { get; set; }
public string CustomerId { get; set; }
public int Total {get; set;}
public virtual Customer Customer{ get; set; }
}
public class Customer : ApplicationUser
{
public long Id {get; set;}
public virtual ICollection<Order> Orders { get; set; }
}
最终,我想返回宇宙中每个客户的列表,即使他们没有订单(左外?),但我也希望每个订单都有一行。所以像:
Customer Order Total
-------- ----- -----
1 null null
2 100 5
2 101 199
3 null null
4 200 299
4 201 399
我遇到的复杂情况是我需要在服务器上执行此操作,因为我需要使用 skip/take 对这些数据进行分页。直接使用Context.Customer.Include(x => x.Order) 不会以我需要它们的方式投影行,以便使用skip/take 进行分页,而且我被困在语法上。
这在直接 LINQ 中是否可行?如果是这样,LINQ 会是什么样子?
提前致谢!
【问题讨论】:
-
您到底想如何分页?您是说要基于数据行进行分页,即使这涉及将单个客户拆分为两个页面?
-
是的,正是@BenM
-
发布的模型中似乎存在一些类型不匹配 -
string在Order中输入CustomerId与在long在Customer中输入Id,因此CustomerId可以' t 是 FK,除非Id不是 PK 或关系已配置为使用其他一些Customer属性作为备用键。你能澄清一下吗?因为使用正确的模型进行左外连接非常容易 - 您需要考虑的是在投影中一些不可为空的字段变为可空。
标签: c# entity-framework entity-framework-core linq-to-entities ef-core-3.1