简体   繁体   English

Dapper处理返回空结果集

[英]Dapper handling returned empty result set

We are using Dapper to map our sql data and so far it has worked very well. 我们正在使用Dapper来映射我们的sql数据,到目前为止它已经运行得很好。 I have a case though where we are doing something similar to: 我有一个案例,虽然我们在做类似的事情:

someObject = con.Query<T>("GetInfoSproc", p, commandType: CommandType.StoredProcedure).Single();

This works great as long as the stored procedure I'm calling returns data. 只要我正在调用的存储过程返回数据,这就很有效。 There are times where the stored procedure might not return a result and return an error in a out parameter. 有时存储过程可能不会返回结果并在out参数中返回错误。 This seems to cause a problem in Dapper because dapper throws the error: 这似乎导致Dapper出现问题,因为dapper会抛出错误:

"When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id" “当使用多映射API时,如果您的ID不是Id,请确保设置splitOn参数”

Is there a way to write the query so it can properly handle the case when an empty result is returned or is this a limitation of Dapper? 有没有办法编写查询,以便它可以正确处理返回空结果的情况或这是Dapper的限制?

SingleOrDefault() is your friend here SingleOrDefault()是你的朋友

Try this: 尝试这个:

someObject = con.Query<T>("GetInfoSproc", p, commandType: CommandType.StoredProcedure).SingleOrDefault();
if (someObject != null)
{
  // operate on your results here
}
return someObject;

also you'll need to make sure T is Nullable 你还需要确保TNullable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM