Entity Framework Return List from Stored Procedure
If your just trying to get a list from a stored procedure you don't need to map anything special.
Just Call it Like this:
var results = dbContext.Database.SqlQuery<int>("SP_YourSP").ToList();
this should return a list of ints
I created this sample stored procedure returning a list of int values:
CREATE PROCEDURE dbo.GetListOfInt
AS BEGIN
SELECT *
FROM
(VALUES (42), (4711), (8088), (80286), (80486), (655235)) AS VT(VC)
END
I then added this stored procedure to my EF .edmx
model and created this Function Import:
Querying the stored procedure shows me that it returns a result set consisting of int
values - I therefore define the return value to be a collection of Scalar: Int32 in the function import dialog.
After that, I can call that stored procedure and get back the results like this:
using (testEntities ctx = new testEntities())
{
ObjectResult<int?> result = ctx.GetListOfInt();
foreach (int intValue in result.AsEnumerable())
{
Console.WriteLine("INT value returned: {0}", intValue);
}
}