.With chaining on LinqMetaData doesn't seem to work

Posts   
 
    
RJReinders
User
Posts: 9
Joined: 04-May-2023
# Posted on: 06-Sep-2023 14:04:31   

I am writing a query where a single Entity should prefetch 2 related Entities (C#). I prefer to keep my queries simple (no QuerySpec, no Prefetch) so followed the guidelines as set out here: https://www.llblgen.com/Documentation/5.8/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Linq/gencode_linq_prefetchpaths.htm#multiple-nodes-at-the-same-level-1

The option to chain .With() statements doesn't seem to work. Consider this:

   public IQueryable<CommandEntity> Build(Guid tenantGuid, Guid commandGuid)
    {
        var db = new LinqMetaData(dataAccessAdapter);
        return (from command in db.Command
                where command.TenantGuid == tenantGuid && command.Guid == commandGuid
                select command)
            .With(cm => cm.Tenant) // is null
            .With(cm2 => cm2.CommandScheduler); // is found
    }

And if I swap the With statements:

    public IQueryable<CommandEntity> Build(Guid tenantGuid, Guid commandGuid)
    {
        var db = new LinqMetaData(dataAccessAdapter);
        return (from command in db.Command
                where command.TenantGuid == tenantGuid && command.Guid == commandGuid
                select command)
            .With(cm2 => cm2.CommandScheduler) // is null
            .With(cm => cm.Tenant); // is found
    }

At first I thought it might not work because the PK for Tenant is used in the where clause, but as demonstrated the problem seems to be that other .With statements other than the last are ignored. I also used different variable names (cm, cm2) because I thought that might cause a clash, but to no avail.

What could be the issue here?

Walaa avatar
Walaa
Support Team
Posts: 14954
Joined: 21-Aug-2005
# Posted on: 06-Sep-2023 16:36:57   

I see there were some fixes related to Linq since v.5.8. Could you please try with the latest release of v.5.10 ?

RJReinders
User
Posts: 9
Joined: 04-May-2023
# Posted on: 06-Sep-2023 16:40:33   

This was done on the latest version as far as I can tell. It also didn't work in slightly older versions (5.8 )

<PackageReference Include="SD.LLBLGen.Pro.ORMSupportClasses" Version="5.10.1" />

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39625
Joined: 17-Aug-2003
# Posted on: 07-Sep-2023 09:13:29   

.With() is a method that should appear in the query once. The example you refer to has a subtle difference with yours: the second .With call is called on the node inside the outer .With call, it's not chained to the outer .With call.

So your query should be:

public IQueryable<CommandEntity> Build(Guid tenantGuid, Guid commandGuid)
{
    var db = new LinqMetaData(dataAccessAdapter);
    return (from command in db.Command
            where command.TenantGuid == tenantGuid && command.Guid == commandGuid
            select command)
        .With(cm => cm.Tenant, cm2 => cm2.CommandScheduler);
}
Frans Bouma | Lead developer LLBLGen Pro
RJReinders
User
Posts: 9
Joined: 04-May-2023
# Posted on: 07-Sep-2023 09:50:54   

Tested and works! I will set this as the accepted answer.