16

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

3
  • 3
    IIRC the generic parameter should be ProfitMargin, not List<ProfitMargin>
    – user47589
    Sep 20, 2016 at 18:56
  • You need "using System.Linq" to get Intellisense to show the Linq extension methods such as ToList()
    – Joe
    Sep 20, 2016 at 18:57
  • For what object you try to call function ToList()?
    – DespeiL
    Sep 20, 2016 at 18:58

2 Answers 2

26

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

4
  • why can't I do .tolistasync? - the .tolist is working now.
    – DinaDee
    Sep 21, 2016 at 13:36
  • 1
    .QueryAsync<t> returns a Task<t> object not a t object. Task objects have their own properties etc on them, one of which is Result which holds your IEnumerable<ProfitMargin> object when execution is complete. Basically you would be trying to call .ToList() on a Task which doesn't have a .ToList() function defined. await interacts a little weird with parenthesis. Think of the await like a cast, to operate on what you are casting to, you have to surround the whole thing with parenthesis. Sep 21, 2016 at 14:19
  • 1
    QueryAsync returns a sequence of type, as per the answer above the response type would be an IEnumerable<IList<ProfileMargin>> not List<ProfileMargin> SOLUTION: List<ProfitMargin> res = await conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()})).ToList() as stated below
    – Raghav
    Jun 27, 2020 at 16:14
  • need Dapper QueryToListAsync<t>
    – mathewsun
    May 30 at 14:53
1

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>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.