Sorry for this simple question .
I have a Stored Procedure that return an int value , I’m trying to call this sp from my asp.net linq to sql project .
int currentRating = db.sproc_GetAverageByPageId(pageId);
But i get this error :
Cannot implicitly convert type `'System.Data.Linq.ISingleResult<PsychoDataLayer.sproc_GetAverageByPageId> to 'int' .`
Edit 1
The solution that friends implied didn’t work . All the time it return 0
For more information i put my stored procedure here :
ALTER procedure [dbo].[sproc_GetAverageByPageId](
@PageId int )
as
select (select sum(score) from votes where pageId = @PageId)/(select count(*) from votes where pageId=@PageId)
You should inspect the
ReturnValueproperty.Perhaps the following works better?
Update: since your stored proc returns a resultset instead of using a
returnstatement the actual data will be available as an element in the enumerable returned bydb.sproc_GetAverageByPageId(pageId). If you inspect theISingleResult<T>type, you’ll see that it inheritsIEnumerable<T>which indicates that you can enumerate the object to get to the data, each element being of typeT.Since the sproc does a
SELECT SUM(*) ...we can count on the resultset to always contain one row. Thus, the following code will give you the first (and only) element in the collection:Now, the type of
sumRowwill beTfrom the interface definition, which in your case isPsychoDataLayer.sproc_GetAverageByPageId. This type hopefully contains a property that contains the actual value you are after.Perhaps you can share with us the layout of the
PsychoDataLayer.sproc_GetAverageByPageIdtype?