Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using dapper map join query to object dto using 'SplitOn'

How do you map a query with dapper to your dto class with relation to another dto class?

Having this dto class structure :

public class ClassA
{
    public string AField1 { get; set; }
    public string AField2 { get; set; }
    public string AField3 { get; set; }
    public ClassB ClassB { get; set; }
}

public class ClassB
{
    public string BField1 { get; set; }
    public string BField2 { get; set; }
    public ClassC ClassC { get; set; }
}

public class ClassC
{
    public string CField1 { get; set; }
    public string CField2 { get; set; }
}

This is my query with some JOIN’s :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT
    [a].[a_field_1] AS 'AField1',
    [a].[a_field_2] AS 'AField2',
    [a].[a_field_3] AS 'AField3',
    [b].[b_field_1] AS 'BField1',
    [b].[b_field_2] AS 'BField2',
    [c].[c_field_1] AS 'CField1',
    [c].[c_field_2] AS 'CField2',
    [d].[d_field_1] AS 'DField1',
FROM
    [dbo].[Table_A] AS [a]
LEFT JOIN
    [dbo].[Table_B] AS [b] ON [a].[table_a_id] = [b].[a_id]
LEFT JOIN
    [dbo].[Table_C] AS [c] ON [b].[table_b_id] = [c].[b_id]
LEFT JOIN
    [dbo].[Table_D] AS [d] ON [c].[table_c_id] = [d].[c_id]
LEFT JOIN
    [dbo].[Table_E] AS [e] ON [d].[table_d_id] = [e].[d_id]

I’m trying to follow the example I found from the Dapper documentation website but no success so far.

var query = await sqlConnection.QueryAsync<ClassA, ClassB, ClassC> (SqlStatementHelper.ReadSqlStatementFile(SqlStatementFileNames.MyQuery), (classA, classB, 
classC) =>
{
    classA.ClassB = classB;
    classB.ClassC = classC;

    return classA;
},
splitOn: "Split on what?");

>Solution :

Change your query to include the fields needed for the split

SELECT
[a].[a_field_1] AS 'AField1',
[a].[a_field_2] AS 'AField2',
[a].[a_field_3] AS 'AField3',
[b].[b_id],
[b].[b_field_1] AS 'BField1',
[b].[b_field_2] AS 'BField2',
[c].]c_id],
[c].[c_field_1] AS 'CField1',
[c].[c_field_2] AS 'CField2',
[d].[d_id],
[d].[d_field_1] AS 'DField1',

so you can set them in the splitOn parameter

....
splitOn: "b_id, c_id, d_id");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading