Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎样才能使我的整洁的结果成为一个列表?_C#_Dapper - Fatal编程技术网

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