内连接在Linq方法中

huangapple go评论64阅读模式
英文:

Inner Join in Linq Method

问题

我有一个查询

var casino = await _context.Casinos
    .Include(c => c.CasinoTableGames).ThenInclude(y => y.TableGame)
    .Include(c => c.CasinoTableGames).ThenInclude(y => y.Manufacturer)
    .Where(y => y.CasinoID == id)
    .FirstOrDefaultAsync(i => i.CasinoID == id);

在CasinoTableGames表中有一列"Active"。我如何只包括在CasinoTableGames表中"Active"为true的行。

英文:

I have a query

var casino = await _context.Casinos
    .Include(c => c.CasinoTableGames).ThenInclude(y => y.TableGame)
    .Include(c => c.CasinoTableGames).ThenInclude(y => y.Manufacturer)
    .Where(y => y.CasinoID == id)
    .FirstOrDefaultAsync(i => i.CasinoID == id);

There is a column "Active" in CasinoTableGames table. How can I include only rows where Active is true in CasinoTableGames table.

答案1

得分: 0

Since EF Core 5 you can use filtered include. Something along these lines:

var casino = await _context.Casinos
    .Include(c => c.CasinoTableGames.Where(ctg => ctg.Active))
        .ThenInclude(y => y.TableGame)
    .Include(c => c.CasinoTableGames).ThenInclude(y => y.Manufacturer)
    .Where(y => y.CasinoID == id)
    .FirstOrDefaultAsync(i => i.CasinoID == id);

Note that this can be affected by previous tracked operations:

In case of tracking queries, results of Filtered Include may be unexpected due to navigation fixup. All relevant entities that have been queried for previously and have been stored in the Change Tracker will be present in the results of Filtered Include query, even if they don't meet the requirements of the filter. Consider using NoTracking queries or re-create the DbContext when using Filtered Include in those situations.

英文:

Since EF Core 5 you can use filtered include. Something along these lines:

var casino = await _context.Casinos
    .Include(c => c.CasinoTableGames.Where(ctg => ctg.Active))
        .ThenInclude(y => y.TableGame)
    .Include(c => c.CasinoTableGames).ThenInclude(y => y.Manufacturer)
    .Where(y => y.CasinoID == id)
    .FirstOrDefaultAsync(i => i.CasinoID == id);

Note that this can be affected by previous tracked operations:

> In case of tracking queries, results of Filtered Include may be unexpected due to navigation fixup. All relevant entities that have been queried for previously and have been stored in the Change Tracker will be present in the results of Filtered Include query, even if they don't meet the requirements of the filter. Consider using NoTracking queries or re-create the DbContext when using Filtered Include in those situations.

huangapple
  • 本文由 发表于 2023年5月15日 15:24:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定