Skip to content

Unnecessary Left Joins Being Generated In Underlying Sql - Possible Solution Provided In Second Comment #161

@CShelton11

Description

@CShelton11

Discussed in #160

Originally posted by CShelton11 November 8, 2022
Sorry for the lengthy post, but trying to be detailed.

I'm experiencing an issue where unnecessary left joins are being generated to child tables in underlying sql being generated.
My setup is outlined below:

Issue By Example:


If I were to invoke the PolicyCase endpoint on my api with the following url:
https://site.com/odata/policyCase?$top=1
I get a response back from the server like the following:

{
    "PolicyCaseId": 1,
    "TicketDetails": [
        { "TicketDetailId": 1, etc... },
        { "TicketDetailId": 2, etc... }
    ]
}

I didn't request the TicketDetails information in my query, but it is being provided anyway.
I can't see that I have configured anything incorrectly, and I can't believe that this isn't something that has been considered already.

Entity Classes:


public class PolicyCase {
    public Int64 PolicyCaseId { get; set; }
    public virtual ICollection<TicketDetail> TicketDetails { get; set; }
}

public class TicketDetail {
    public Int64 TicketDetailId { get; set; }
    public virtual PolicyCase PolicyCase { get; set; }
}

Dto Classes:


public class PolicyCaseDto {
    public Int64 PolicyCaseId { get; set; }
    public ICollection<TicketDetailDto> TicketDetails { get; set; }
}

public class TicketDetailDto {
    public Int64 TicketDetailId { get; set; }
    public PolicyCaseDto PolicyCase { get; set; }
}

Profiles:


public class PolicyCaseProfile : Profile {
    public PolicyCaseProfile() {
        CreateMap<PolicyCase, PolicyCaseDto>(MemberList.Destination).ReverseMap();
    }
}

public class TicketDetailProfile : Profile {
    public TicketDetailProfile() {
        CreateMap<TicketDetail, TicketDetailDto>(MemberList.Destination).ReverseMap();
    }
}

Mappers:


_mapper = new MapperConfiguration(a => a.AddMaps(typeof(PolicyCaseProfile))).CreateMapper();

Extension Method:


public static IQueryable<PolicyCaseDto> ToDtoQueryable(this IQueryable<PolicyCase> queryable, ODataQueryOptions<PolicyCaseDto> options) {
    return queryable.GetQuery(_mapper, options);
}

Note - If I were to do the following: queryable.GetQuery(_mapper, options).ToList();
It returns all of the TicketDetails for the 1 record I filtered for.

I'm guessing it has something to do with the projection including a ToList in there somewhere, but I have to believe that this has been considered already (Like an option telling the projection to issue a Select statement instead of a ToList???).
How do I avoid this issue????

Any help is greatly appreciated...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions