Search code examples
c#sqlasp.net-coredapper

QueryMultiple in Dapper only showing the first list


I'm using dapper to try and send multiple querys at the same time. I have 2 different querys that I want to use. However, no matter what I try, I can only get the method to find 1 of the querys.

Firstly, here's the controller:

[HttpGet]
public async Task<IActionResult> Test(RequestOverview model)
{

    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    try
    {
        await _request.Test(model);
    }
    catch (Exception ex)
    {

        return BadRequest(ex.Message);
    }
    return Ok();
}

Interface:

Task<bool> Test(RequestOverview model);

Model:

public class TestT
{
    public string vehicleRegNr { get; set; } = "";
}

public class TestE
{
    public string dealerName { get; set; } = "";
}

Sidenote: The name of the entire class is RequestOverview

Now, here are some of my attempts in the method that uses dapper:

// attempt 1
public async Task<bool> Test(RequestOverview model)
{

    string query = @"SELECT TOP 5 RegNumber as vehicleRegNr FROM [dbo].[Vehicle]; 
                     SELECT TOP 5 Retailer.Name as dealerName FROM [dbo].[Retailer]";

    using (var multi = _sqlconnection.QueryMultiple(query, null))
    {
        List<TestT> list1 = multi.Read<TestT>().AsList();
        List<TestE> list2 = multi.Read<TestE>().AsList();
    }

    return true;
}



attempt 2//
public async Task<bool> Test(TestT model)
{
    string query = @"SELECT TOP 5 RegNumber as vehicleRegNr FROM [dbo].[Vehicle]; 
                     SELECT TOP 5 Retailer.Name as dealerName FROM [dbo].[Retailer]";

    using (var multi = _sqlconnection.QueryMultiple(query, null))
    {
        List<dynamic> list1 = multi.Read<dynamic>().AsList();
        List<dynamic> list2 = multi.Read<dynamic>().AsList();
    }

    return true;
}

(In attempt 2 I use only one model class TestT that has vehicleRegNr and dealerName as parameters)

However, no matter what I get this output when debugging: enter image description here

List 2 always ends up null, anyone knows why? Thankful for help. (Sidenote: List 1 contains the vehicleRegNr)


Solution

  • I could not reproduce that problem, created an example https://dotnetfiddle.net/j0L0kd and QueryMultiple works as expected.

    And you can see the results: enter image description here

    So I suspect either you check list2 values before multi.Read<dynamic>().AsList(); executed or second query returns nothing :)