当 where/select 中的任何一个不存在于查询上下文中时,如何在包含 ThenInclude 的查询中指定 where 和 select?
How to specify where and select in a query containing ThenInclude, when either of where/select is not present in the query context?
我有 3 个 table:
合同、事项和索赔。每个 table 与以下 table.
有 1:N 关系
我想select声明名称,其中合同con_name = 'C109K'
var claims = await _context.Claims.AsNoTracking()
.Include(cl=>cl.Matter)
.ThenInclude(mat=>mat.Contract)
.Where(con=>con.con_name=='C109K')
.Select(cl=>new{name=cl.cl_name})
错误是 where 子句无法找到 con_name 因为上下文是声明。
我也尝试了相反的方法——即——从合同开始查询。但在那种情况下,select 无法获取声明名称,因为查询上下文是合同。
如何正确编写此查询?
没有模型很难确定,但我认为这应该有效:
var claims = await _context.Claims.AsNoTracking()
.Include(cl=>cl.Matter)
.ThenInclude(mat=>mat.Contract)
.Where(cl=>cl.Matter.Contract.con_name=='C109K') // <-- find the contract associated with the claim
.Select(cl=>new{name=cl.cl_name})
.Include()
和.ThenInclude()
方法用于加载相关实体(参见eager loading上的文档)。他们要求 EF Core 从数据库中检索数据,并将其加载到关联的实体中。
例如你的情况:
var claim = (await _context.Claims.AsNoTracking()).First();
var matter = claim.Matter; // matter will always be null, because it was no loaded
claim = (await _context.Claims.Include(cl=>cl.Matter).AsNoTracking()).First();
matter = claim.Matter; // the claim's matter is loaded thanks to the Include call
var contract = matter.Contract; // contract is null because it was not loaded
claim = (await _context.Claims.Include(cl=>cl.Matter).ThenInclude(mat=>mat.Contract).AsNoTracking()).First();
matter = claim.Matter; // the claim's matter is loaded thanks to the Include call
contract = matter.Contract; // contract is loaded thanks to the ThenInclude call
这些方法允许精细控制从数据库中检索哪些数据:有时您不需要所有数据,只需要顶级视图。
然而,在您的情况下,不需要这些调用,因为您不检索链接的实体:您只需要它们进行过滤,并且过滤仅由 运行 适当的 SQL查询。
我有 3 个 table:
合同、事项和索赔。每个 table 与以下 table.
有 1:N 关系我想select声明名称,其中合同con_name = 'C109K'
var claims = await _context.Claims.AsNoTracking()
.Include(cl=>cl.Matter)
.ThenInclude(mat=>mat.Contract)
.Where(con=>con.con_name=='C109K')
.Select(cl=>new{name=cl.cl_name})
错误是 where 子句无法找到 con_name 因为上下文是声明。
我也尝试了相反的方法——即——从合同开始查询。但在那种情况下,select 无法获取声明名称,因为查询上下文是合同。
如何正确编写此查询?
没有模型很难确定,但我认为这应该有效:
var claims = await _context.Claims.AsNoTracking()
.Include(cl=>cl.Matter)
.ThenInclude(mat=>mat.Contract)
.Where(cl=>cl.Matter.Contract.con_name=='C109K') // <-- find the contract associated with the claim
.Select(cl=>new{name=cl.cl_name})
.Include()
和.ThenInclude()
方法用于加载相关实体(参见eager loading上的文档)。他们要求 EF Core 从数据库中检索数据,并将其加载到关联的实体中。
例如你的情况:
var claim = (await _context.Claims.AsNoTracking()).First();
var matter = claim.Matter; // matter will always be null, because it was no loaded
claim = (await _context.Claims.Include(cl=>cl.Matter).AsNoTracking()).First();
matter = claim.Matter; // the claim's matter is loaded thanks to the Include call
var contract = matter.Contract; // contract is null because it was not loaded
claim = (await _context.Claims.Include(cl=>cl.Matter).ThenInclude(mat=>mat.Contract).AsNoTracking()).First();
matter = claim.Matter; // the claim's matter is loaded thanks to the Include call
contract = matter.Contract; // contract is loaded thanks to the ThenInclude call
这些方法允许精细控制从数据库中检索哪些数据:有时您不需要所有数据,只需要顶级视图。
然而,在您的情况下,不需要这些调用,因为您不检索链接的实体:您只需要它们进行过滤,并且过滤仅由 运行 适当的 SQL查询。