C# 我怎样才能使我的整洁的结果成为一个列表?
为什么我不能在上面添加C# 我怎样才能使我的整洁的结果成为一个列表?,c#,dapper,C#,Dapper,为什么我不能在上面添加.ToList()?Intellisense唯一允许的是.ToString() /。。 string sqlQuery=“选择sum(SellingPrice)作为SellingPrice,sum(MarkupPercent)作为MarkupPercent,sum(MarkupAmount)作为ProfitMargins中的MarkupAmount,其中,@QuoteId group by multiplier中的QuoteId”; { List profitMargin=
.ToList()
?Intellisense唯一允许的是.ToString()
/。。
string sqlQuery=“选择sum(SellingPrice)作为SellingPrice,sum(MarkupPercent)作为MarkupPercent,sum(MarkupAmount)作为ProfitMargins中的MarkupAmount,其中,@QuoteId group by multiplier中的QuoteId”;
{
List profitMargin=(List)WAIT conn.QueryAsync(sqlQuery,new{QuoteId=QuoteIds.ToArray()})//希望在此处添加.ToList();
回报利润边际;
}
//..
更新
我认为问题与conn.querySync(conn是context.Database.Connection.ConnectionString)有关,而不是与context.Database.SqlQuery有关。请尝试更改为此
List<ProfitMargin> profitMargin = (await conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()})).ToList();
List profitMargin=(wait conn.QueryAsync(sqlQuery,new{QuoteId=QuoteIds.ToArray()})).ToList();
或
var results=await conn.querysync(sqlQuery,new{QuoteId=QuoteIds.ToArray()});
List profitMargin=results.ToList();
我认为您试图调用.ToList()
尝试以下操作是在点击任务
对象:
List<ProfitMargin> profitMargin = new List<ProfitMargin>(await conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()}));
List profitMargin=new List(等待conn.querysync(sqlQuery,new{QuoteId=QuoteIds.ToArray()}));
或
List profitMargin=(wait conn.QueryAsync(sqlQuery,new{QuoteId=QuoteIds.ToArray()})).ToList();
正如@Amy所说,您需要使用
conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()}))
conn.querysync(sqlQuery,新的{QuoteId=QuoteIds.ToArray()}))
它返回一个
任务
,因此,在等待对IEnumerable
IIRC求值时,泛型参数应该是ProfitMargin
,而不是列表
,您需要“使用System.Linq”来获取Intellisense,以显示Linq扩展方法,例如您试图调用函数ToList()的对象的ToList()?为什么我做不到?ToListSync?-.tolist现在正在工作。.QueryAsync
返回一个任务
对象,而不是t
对象。任务对象有自己的属性等,其中一个是Result
,它在执行完成时保存IEnumerable
对象。基本上,您将尝试在未定义.ToList()
函数的任务上调用.ToList()
<代码>等待
与括号的交互有点奇怪。将await
想象成一个cast,要对您要强制转换的内容进行操作,您必须用括号将整个内容括起来。QueryAsync
返回一个类型序列,根据上面的答案,响应类型将是IEnumerable
而不是List
解决方案:List res=await conn.QueryAsync(sqlQuery,new{QuoteId=QuoteIds.ToArray()})).ToList()
如下所述
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();
conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()}))