I am having a problem with getting a result when trying to get objects with multiple levels. This is what I am trying to do roughly:
_context.Investors.Where(s => s.Id == userId)
.Include(c => c.Coins) //only want this if some kind of flag is given by the user.
.ThenInclude(ct => ct.CoinType)
.Include(c => c.Bricks) //only want this if some kind of flag is given by the user.
Essentially I am getting a lot of flags indicating if I should include parts of the object. I got it almost to work. Like this:
_context.Investors.Where(s => s.Id == userId)
.Select(i => new
{
i,
Bricks = (details & GapiInvestorFlags.Bricks) != GapiInvestorFlags.Bricks ? null : i.Bricks,
Offers = (details & GapiInvestorFlags.Offers) != GapiInvestorFlags.Offers ? null : i.Offers,
Coins = (details & GapiInvestorFlags.Coins) != GapiInvestorFlags.Coins ? null : i.Coins,
CoinTransactions = (details & GapiInvestorFlags.CoinTransactions) != GapiInvestorFlags.CoinTransactions ? null : i.CoinTransactions,
OfferTransactions = (details & GapiInvestorFlags.OfferTransactions) != GapiInvestorFlags.OfferTransactions ? null : i.OfferTransactions,
BuyTransactions = (details & GapiInvestorFlags.BuyTransactions) != GapiInvestorFlags.BuyTransactions ? null : i.BuyTransactions,
SellTransactions = (details & GapiInvestorFlags.SellTransactions) != GapiInvestorFlags.SellTransactions ? null : i.SellTransactions
}).AsEnumerable()
.Select(e => e.i).FirstOrDefault();
This works except that the Coins section also has a cointype in it so I need to include it too. But when I add my code, the whole section stops working.
This is what I tried:
_context.Investors.Where(s => s.Id == userId)
.Include(c => c.Coins)
.ThenInclude(ct => ct.CoinType)
.Select(i => new
{
i,
Bricks = (details & GapiInvestorFlags.Bricks) != GapiInvestorFlags.Bricks ? null : i.Bricks,
Offers = (details & GapiInvestorFlags.Offers) != GapiInvestorFlags.Offers ? null : i.Offers,
Coins = (details & GapiInvestorFlags.Coins) != GapiInvestorFlags.Coins ? null : i.Coins.Select(c => new { c, c.CoinType }).ToList(),
CoinTransactions = (details & GapiInvestorFlags.CoinTransactions) != GapiInvestorFlags.CoinTransactions ? null : i.CoinTransactions,
OfferTransactions = (details & GapiInvestorFlags.OfferTransactions) != GapiInvestorFlags.OfferTransactions ? null : i.OfferTransactions,
BuyTransactions = (details & GapiInvestorFlags.BuyTransactions) != GapiInvestorFlags.BuyTransactions ? null : i.BuyTransactions,
SellTransactions = (details & GapiInvestorFlags.SellTransactions) != GapiInvestorFlags.SellTransactions ? null : i.SellTransactions
}).AsEnumerable()
.Select(e => e.i).FirstOrDefault();
I really can't tell why it does not work.
Basically when I change:
i.Coins
To
i.Coins.Select(c => new { c, c.CoinType }).ToList()
it stops working.
The technique you are using is not really explicit loading (
Include
/ThenInclude
), but trick based on projection and EF Core navigation property fix-up, so I can't say why it stops working. EF Core still processes projections and includes differently, so it might be a defect in the current processing.Implementing conditional include at the root query level is relatively easy. Note that the
Include
method starts from (is defined for)IQueryable<TEntity>
and the returnedIIncludableQueryable<TEntity, TPreviousProperty>>
is alsoIQueryable<TEntity>
. Which means you can keepIQueryable<T>
query variable and apply conditional transformations similar to chainedWhere
operators.To make that easier, you could create a custom helper extension method like this:
and use it like this:
If you need something similar for the nested levels (
ThenInclude
), then add the following 2 extension methods:which will allow you to use something like this: