C
C#3y ago
kopuo

❔ How to Select Data with Included nested objects?

When extracting data from the database for the main object, I want to extract only the necessary data. My code now:
_dbContext.Units // I need only two fields from here, not whole object
.Include(p => p.Shops)
.ThenInclude(p => p.Department)
.ThenInclude(p => p.Products.Where(i => i.Availability == available))
.AsNoTracking()
.SingleOrDefaultAsync(p => p.UnitId == unitId);
_dbContext.Units // I need only two fields from here, not whole object
.Include(p => p.Shops)
.ThenInclude(p => p.Department)
.ThenInclude(p => p.Products.Where(i => i.Availability == available))
.AsNoTracking()
.SingleOrDefaultAsync(p => p.UnitId == unitId);
I want something like this:
_dbContext.Units
.Select(x => new
{
UnitId = x.UnitId,
Name = x.Name
})
.Include(p => p.Shops)
.ThenInclude(p => p.Department)
.ThenInclude(p => p.Products.Where(i => i.Availability == available))
.AsNoTracking()
.SingleOrDefaultAsync(p => p.UnitId == unitId);
_dbContext.Units
.Select(x => new
{
UnitId = x.UnitId,
Name = x.Name
})
.Include(p => p.Shops)
.ThenInclude(p => p.Department)
.ThenInclude(p => p.Products.Where(i => i.Availability == available))
.AsNoTracking()
.SingleOrDefaultAsync(p => p.UnitId == unitId);
2 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server