Dapper QueryAsync,返回一个列表

时间:2018-12-01 19:25:03

标签: c# mysql dapper

我在C#中有此任务,我想使用dapper在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();
        }            
    }

问题在于它不返回任何内容。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

我设法通过以下方式解决了这个问题:

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();
       }                  
    }

问题是我无法将generic.Ienumerable转换为generic.List

谢谢!

答案 1 :(得分:0)

public virtual async Task<T> QueryFirstAsync<T>(string strSql, object param)
{
    using (IDbConnection conn = Connection)
    {
        var res = await conn.QueryAsync<T>(strSql, param);
        return res.FirstOrDefault<T>();
    }
}