What can I do to make my Dapper return a list?

c# dapper

Question

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

1
8
9/20/2016 6:58:55 PM

Accepted Answer

Try changing to this.

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

Or

var results = await conn.QueryAsync<List<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()

10
9/20/2016 7:04:51 PM

Popular Answer

Try:

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

or

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

As @Amy stated, you need to use

conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()}))

which returns a Task<IEnumerable<ProfitMargin>> thus upon awaiting evaluates to an IEnumerable<ProfitMargin>.



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