Entity Framework Return List from Stored Procedure

c# entity-framework stored-procedures


I'm trying to return a list of int from a stored procedure in Entity Framework.

I created the stored procedure fine, and added it into Entity Framework. I'm trying to bind it to a complex type of but when I open the function import.

It auto generates a complex type that only returns an int instead of a results set.

Does anyone know how I can import an entity that returns a list as a result set?

3/12/2015 2:50:22 PM

Accepted Answer

I created this sample stored procedure returning a list of int values:

    SELECT *
    (VALUES (42), (4711), (8088), (80286), (80486), (655235)) AS VT(VC)

I then added this stored procedure to my EF .edmx model and created this Function Import:

enter image description here

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);
3/12/2015 3:12:47 PM

Popular Answer

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

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow