英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论