我怎样才能得到我衣冠楚楚的结果是一个列表

本文关键字:列表 一个 结果是 衣冠楚楚 | 更新日期: 2023-09-27 18:08:02

为什么我不能在此添加.ToList() ?智能感知唯一允许的是.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;
}
//..

更新

我认为问题与conn.queryasync (conn是context.Database.Connection.ConnectionString)有关,而不是context.Database.SqlQuery

我怎样才能得到我衣冠楚楚的结果是一个列表

试着改一下

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

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

我认为你在尝试调用.ToList()时击中了Task对象

尝试:

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

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

正如@Amy所说,你需要使用

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

返回Task<IEnumerable<ProfitMargin>>,因此等待时求值为IEnumerable<ProfitMargin>