无法将类型'System.Data.Linq.ISingleResult <CustomeProcedureName>隐式转换为'int'

Mos*_*afa 3 linq asp.net types linq-to-sql

对不起这个简单的问题.

我有一个存储过程返回一个int值,我试图将这个sp从我的asp.net linq调用到sql项目.

int currentRating = db.sproc_GetAverageByPageId(pageId);
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

Cannot implicitly convert type `'System.Data.Linq.ISingleResult<PsychoDataLayer.sproc_GetAverageByPageId> to 'int' .`
Run Code Online (Sandbox Code Playgroud)

编辑1 朋友暗示的解决方案不起作用.它一直返回0更多信息我把我的存储过程放在这里:

ALTER procedure [dbo].[sproc_GetAverageByPageId](
@PageId int )
as 
select (select sum(score) from votes where pageId = @PageId)/(select count(*) from votes where pageId=@PageId)
Run Code Online (Sandbox Code Playgroud)

Pet*_*old 8

您应检查ReturnValue属性.

也许以下工作更好?

int currentRating = (int)db.sproc_GetAverageByPageId(pageId).ReturnValue;
Run Code Online (Sandbox Code Playgroud)

更新:由于您的存储过程返回结果集而不是使用return语句,因此实际数据将作为返回的枚举中的元素提供db.sproc_GetAverageByPageId(pageId).如果你检查ISingleResult<T>类型,你会看到,它继承IEnumerable<T>这表明你可以枚举对象来获取数据,每一个元素是类型T.

由于sproc执行了一次,SELECT SUM(*) ...我们可以指望结果集始终包含一行.因此,以下代码将为您提供集合中的第一个(也是唯一的)元素:

var sumRow = db.sproc_GetAverageByPageId(pageId).Single();
Run Code Online (Sandbox Code Playgroud)

现在,类型sumRowT来自接口定义,在您的情况下PsychoDataLayer.sproc_GetAverageByPageId.希望这种类型包含一个包含您所追求的实际值的属性.

也许您可以与我们分享该PsychoDataLayer.sproc_GetAverageByPageId类型的布局?