none
return a single integer value from Linq RRS feed

  • Question

  •  

    Let's say that I've added a stored procedure to my DataContext that is meant to return a single integer value.  How can I use the automatically generated method, which returns ISingleResult, to supply an integer.

     

    I'm thinking something like this in my business layer.

     

    public static int GetIdByParameter(string myParameter)

    {

    //this returns an ISingleResult, not an integer

    return DataContext.MyStoredProc(myParameter);

    }

    Monday, March 17, 2008 8:08 PM

Answers

  • Try this:

     

    Code Snippet

    public static int GetIdByParameter(string myParameter)

    {

    //this returns an integer

    return DataContext.MyStoredProc(myParameter).First().Column1;

    }

     

     

    You can rename Column1 to whatever you like, but the designer/sqlmetal autoname it to Column1 by default when generating the partial class accessor for the stored procedure.  Of course, you will first want to validate the result before using it, especially if the result is nullable.

     

     

    - Josh

    Saturday, May 10, 2008 2:09 AM
  • Examine the 'ReturnValue' property of the ISingleResult result type.

     

    --Samir

     

     

    Monday, May 12, 2008 5:20 PM

All replies

  • Try this:

     

    Code Snippet

    public static int GetIdByParameter(string myParameter)

    {

    //this returns an integer

    return DataContext.MyStoredProc(myParameter).First().Column1;

    }

     

     

    You can rename Column1 to whatever you like, but the designer/sqlmetal autoname it to Column1 by default when generating the partial class accessor for the stored procedure.  Of course, you will first want to validate the result before using it, especially if the result is nullable.

     

     

    - Josh

    Saturday, May 10, 2008 2:09 AM
  • Examine the 'ReturnValue' property of the ISingleResult result type.

     

    --Samir

     

     

    Monday, May 12, 2008 5:20 PM