Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a stored procedure that has input Id and Loc and returns Id,Loc and Description So i made a class in C# called RetrieveDE that has these three returned fields

Then i edited my stored procedure to return two selects instead of one. And now i don't know how to deal with both selects

What I have tried:

Before i edited the stored procedure:

<pre>
using (TableDBContext table= new TableDBContext())
{
   var result = table.Database.SqlQuery<RetrieveDE>("exec usp_Retrieve 
                @Id, @Loc"
                    , new SqlParameter("Id", id)  //id and loc are input to the method
                    , new SqlParameter("Loc", loc))).ToList();

   return result;   //RetrieveDE has the name of fields that are returned by the 
                             stored procedure which are Id,Loc and Description
}


then i use the result in controller

Now i edited my stored procedure to return two results, i mean there is now 2 selects in the stored Procedure. what should i do in the code above to be able to deal with these two results.
The other select returns DayId and Day?
Posted
Updated 22-Apr-20 0:49am
v2

1 solution

There is no native support for multiple result sets in Entity Framework.

If you're using EF6, you can get the ObjectContext and manually translate the results, as described in this SO answer[^]:
C#
using (TableDBContext context = new TableDBContext())
{
    var objectContext = ((IObjectContextAdapter)context).ObjectContext;
    
    using (var command = context.Database.Connection.CreateCommand())
    {
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.CommandText = "dbo.usp_Retrieve";
        command.Parameters.AddWithValue("@Id", id);
        command.Parameters.AddWithValue("@Loc", loc);
        
        context.Connection.Open();
        
        using (var reader = command.ExecuteReader())
        {
            var firstResultSet = objectContext.Translate<RetrieveDE1>(reader, "Set1", MergeOption.AppendOnly).ToList();
            
            reader.NextResult();
            
            var secondResultSet = objectContext.Translate<RetrieveDE2>(reader, "Set2", MergeOption.AppendOnly).ToList();
            
            return new { firstResultSet, secondResultSet };
        }
    }
}
EF Core doesn't have that option, and doesn't support materializing objects which aren't part of the model. If you're using a "query type" / "keyless entity type"[^], you may be able to use something like this code[^] to materialize the results. Otherwise, you'll have to load them manually.
 
Share this answer
 
v2
Comments
Member 14800672 22-Apr-20 8:01am    
what is the return type?
Richard Deeming 22-Apr-20 8:04am    
Whatever type you've created to represent the two result sets. In this case, I've returned an anonymous type, but you'll probably want to create a specific type to hold both lists instead.
Member 14800672 22-Apr-20 14:51pm    
thank you so much
but now in controller class how i'm going to get each one, right now the two lists are retrieved but what to write in controller to deal with each one of them?
Richard Deeming 22-Apr-20 15:48pm    
Sorry, that makes no sense. You're asking how to write code to "deal with" two lists. But you haven't explained what you're trying to do with them, or where you're stuck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900