Search code examples
c#dapper

How can I get my dapper result to be a List?


Why I can't add a .ToList() on this? The only thing Intellisense is allowing is .ToString().

//..
string sqlQuery = "SELECT sum(SellingPrice) as SellingPrice, sum(MarkupPercent) as MarkupPercent, sum(MarkupAmount) as MarkupAmount FROM ProfitMargins WHERE QuoteId in @QuoteId group by multiplier";
{
    List<ProfitMargin> profitMargin = (List<ProfitMargin>)await conn.QueryAsync<List<ProfitMargin>>(sqlQuery, new { QuoteId = QuoteIds.ToArray()})  //would like to add .ToList() here;

    return profitMargin;
}
//..

UPDATE

I believe the problem has to do with conn.queryasync (conn is context.Database.Connection.ConnectionString) instead of context.Database.SqlQuery


Solution

  • Try changing to this.

    List<ProfitMargin> profitMargin = (await conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()})).ToList();
    

    Or

    var results = await conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()});
    List<ProfitMargin> profitMargin = results.ToList();
    

    I think you are hitting the Task object with your attempts at calling .ToList()