-1

I have GetClientDocumentRequestResponse response dto. In this I have GetDocumentRequestClientResponse DTO which property name is client and GetDocumentRequestClientResponse DTO which property name is RequestedBy.

Now, when I run the query in Dapper, the values are not populated in client and RequestedBy object which are properties of GetClientDocumentRequestResponse. Other properties of this class do get values.

Here is the GetClientDocumentRequestResponse class:

public sealed class GetClientDocumentRequestResponse
{
    public int RequestId { get; set; }
            
    public int ClientId { get; set; }
            
    public int RequestedById { get; set; }
            
    public string DocumentName { get; set; } = string.Empty;
    
    public GetDocumentRequestClientResponse Client { get; set; } = new GetDocumentRequestClientResponse();
        
    public GetDocumentRequestClientResponse RequestedBy { get; set; } = new GetDocumentRequestClientResponse();
}

public class GetDocumentRequestClientResponse
{
    public int ClientId { get; set; }
    public string FirstName { get; set; } = string.Empty;
    public string LastName { get; set; } = string.Empty;
    public string CompanyName { get; set; } = string.Empty;
}

Here is how I am running my Dapper queries:

const string sql = $@"SELECT TOP 1
    cdr.RequestId,
    cdr.ClientId,
    cdr.RequestedById,
    cdr.DocumentName,

    client.ClientId AS Client_ClientId,
    client.CompanyName AS Client_CompanyName,
    client.FirstName AS Client_FirstName,
    client.LastName AS Client_LastName,
    
    requestedBy.ClientId AS RequestedBy_ClientId,
    requestedBy.CompanyName AS RequestedBy_CompanyName,
    requestedBy.FirstName AS RequestedBy_FirstName,
    requestedBy.LastName AS RequestedBy_LastName,

FROM ClientDocumentRequest cdr
LEFT JOIN ClientInfo client ON cdr.ClientId = client.ClientId
LEFT JOIN ClientInfo requestedBy ON cdr.RequestedById = requestedBy.ClientId
WHERE cdr.RequestId = @RequestId
  AND cdr.TenantId = @TenantId
;";

var response = (await connection.QueryAsync<GetClientDocumentRequestResponse, GetDocumentRequestClientResponse, 
    GetDocumentRequestClientResponse, GetClientDocumentRequestResponse>(
        sql,
        (cdr, client, requestedBy) =>
        {
            cdr.Client = client;
            cdr.RequestedBy = requestedBy;
            return cdr;
        },
        parameters,
        splitOn: "Client_ClientId,RequestedBy_ClientId"
    )).FirstOrDefault();

When running this query on the database, I am getting values in Client and RequestedBy objects as well.

1 Answer 1

0

Here are changes which fixed this issue.

const string sql = $@"SELECT TOP 1
    cdr.RequestId AS {nameof(GetClientDocumentRequestResponse.RequestId)},
    cdr.ClientId AS {nameof(GetClientDocumentRequestResponse.ClientId)},
    cdr.RequestedById AS {nameof(GetClientDocumentRequestResponse.RequestedById)},
    cdr.DocumentName AS {nameof(GetClientDocumentRequestResponse.DocumentName)},


    client.ClientId AS {nameof(GetDocumentRequestClientResponse.ClientId)},
    client.CompanyName AS {nameof(GetDocumentRequestClientResponse.CompanyName)},
    client.FirstName AS {nameof(GetDocumentRequestClientResponse.FirstName)},
    client.LastName AS {nameof(GetDocumentRequestClientResponse.LastName)},
    
    requestedBy.ClientId AS {nameof(GetDocumentRequestClientResponse.ClientId)},
    requestedBy.CompanyName AS {nameof(GetDocumentRequestClientResponse.CompanyName)},
    requestedBy.FirstName AS {nameof(GetDocumentRequestClientResponse.FirstName)},
    requestedBy.LastName AS {nameof(GetDocumentRequestClientResponse.LastName)},

FROM ClientDocumentRequest cdr
LEFT JOIN ClientInfo client ON cdr.ClientId = client.ClientId
LEFT JOIN ClientInfo requestedBy ON cdr.RequestedById = requestedBy.ClientId
WHERE cdr.RequestId = @RequestId
  AND cdr.TenantId = @TenantId
;";

var response = (await connection.QueryAsync<GetClientDocumentRequestResponse, GetDocumentRequestClientResponse, GetDocumentRequestClientResponse, GetClientDocumentRequestResponse>(
    sql,
    (cdr, client, requestedBy) =>
    {
        cdr.Client = client;
        cdr.RequestedBy = requestedBy;
        return cdr;
    },
    parameters,
    splitOn: "ClientId, ClientId"
)).FirstOrDefault();
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain in English what those changes are?
I simply changes naming convention of Aliases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.