Convert ObjectResult<int?> into List<int>

C Sharper

I have written a stored procedure :-

CREATE PROCEDURE GET_USERSTORYDATA_EXISTS
    @MyWizardUserStoryId INT
   ,@ProjectId INT
   ,@StoryTitle NVARCHAR(MAX)
AS
BEGIN
    SET NOCOUNT ON;
    IF EXISTS (SELECT 1 FROM USERSTORY WHERE [MyWizardUserStoryId]=@MyWizardUserStoryId AND ProjectId=@ProjectId)
    BEGIN
        IF EXISTS (SELECT 1 FROM USERSTORY WHERE [MyWizardUserStoryId]=@MyWizardUserStoryId AND ProjectId=@ProjectId AND Title=@StoryTitle)
        BEGIN
            SELECT 0;
        END
        ELSE
         BEGIN
            SELECT 1;
        END
    END
    ELSE
         BEGIN
            SELECT 1;
        END

END
GO

I have consumed this procedure via Entity framework :-

List<Int32>lst=context.GET_USERSTORYDATA_EXISTS(MyWizardUserStoryId,ProjectId,storyTitle);

but its giving me error:-

Can not implicitly convert type 'ObjectResult<int?>' to List<int>
DavidG

Since ObjectResult<T> implements IEnumerable<T> you can use ToList. However, you also need to convert from nullable int. Easiest way would be to project using a Linq Select and then ToList. This code will replace any null values with zero:

List<Int32> lst = context
    .GET_USERSTORYDATA_EXISTS(MyWizardUserStoryId, ProjectId, storyTitle)
    .Select(i => i.GetValueOrDefault(0))
    .ToList();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related