简体   繁体   English

Dapper QueryAsync,返回一个列表

[英]Dapper QueryAsync, return a list

I have this task in C# with which i want to call a stored procedure in MySql using 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();
        }            
    }

The problem is that it doesn't return anything.问题是它不返回任何东西。

Can someone help me please?有人能帮助我吗?

I solved the issue like this:我解决了这样的问题:

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

The problem was that I could not convert generic.Ienumerable to generic.List.问题是我无法将 generic.Ienumerable 转换为 generic.List。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM