When I have the following EF query which is failing to retrieve the data from the db.
var results = await (from a in agreements let showPasscode = <snipped> let passcode = <snipped> select new Result( a.Id, a.UserId, a.CurrentCycle().StartsOn, passcode, showPasscode) ) .ToListAsync(cancellationToken);
And the code for CurrentCyle
public static AgreementCycle CurrentCycle(this Agreement agreement) => agreement.Cycles .OrderByDescending(x => x.StartsOn) .FirstOrDefault();
Now when I run this (and put a breakpoint) in the extension method, I get the following:
Now I KNOW the database -does- have the linked data, for this item.
I'm guessing this might be related to maybe some delayed expression handling? Like, extension methods are ran after the db results are executed and the property hasn't been "Included" or something?
If I -manually- add the content of the extension method in, I get the correct result:
var results = await (from a in agreements let showPasscode = <snipped> let passcode = <snipped> select new Result( a.Id, a.UserId, a.Cycles.OrderByDescending(c => c.StartsOn).FirstOrDefault().StartsOn, // <-- HERE passcode, showPasscode) ) .ToListAsync(cancellationToken);
So I'm not sure what needs to be done. Now, if this is all about missing 'Include(..)' statements .. this might not work because of the format of my query - it's like it's the old linq-to-sql format. Why? because of those 2x let
commands. I love those and it is really helping make my query easier/simpler. I'm not sure I can do that in any way?