How to return a list using Dapper QueryAsync?

c# dapper mysql

Question

I have a C# job where I want to use dapper to call a stored procedure in MySql.

public async Task<List<StatItemListViewModel>> GetTable()
    {
        using (MySqlConnection connection = new MySqlConnection(Helper.CnnVal("SampleDB")))
        {
            var results =await connection.QueryAsync<List<StatItemListViewModel>>("Call MainResult_Statistic(@sDate, @eDate)", new { sDate = "2018-11-01", eDate = "2018-11-30" });

            return results.FirstOrDefault();
        }            
    }

The issue is that it gives no results.

Could someone please assist me?

1
0
12/1/2018 7:25:03 PM

Accepted Answer

I was able to resolve the problem by performing the following:

public async Task<IEnumerable<StatItemListViewModel>> GetTable(string sDate, string eDate)
    {
        using (MySqlConnection connection = new MySqlConnection(Helper.CnnVal("SampleDB")))
        {
            var results = await connection.QueryAsync<StatItemListViewModel>("Call MainResult_Statistic(@sDate, @eDate)", 
                new { sDate, eDate });

            return results.ToList();
       }                  
    }

I was unable to convert generic, which was the issue. generic to ienumerable List

I'm grateful.

4
12/1/2018 8:28:22 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow