1

Quite often our systems call stored procedures which output multiple tables worth of results. Previously we used XML outputs to get each table and relate them correctly using XSLT. If I were using ASP.NET MVC with LINQ calling a stored procedure, how do I get each of the tables and then output the data as necessary?

3
  • Can you elaborate a bit more on your question, Do you want to display this data from the stored procedure on a web page? Commented Apr 21, 2009 at 13:10
  • Yeah, I'm trying to output three separate tables worth of data on a single web page using LINQ. Commented Apr 21, 2009 at 13:15
  • Kezzer - u should also ask for the answer in VB.NET and add that tag, 'cause I know you're in VB world and struggling to translate C# to VB.NET. Commented Apr 23, 2009 at 4:33

1 Answer 1

3

There's an article here about LINQ to SQL and stored procedures, especially the section 'Handling Multiple Result Shapes from SPROCs': LINQ to SQL - Retrieving Data Using Stored Procedures.

Is that useful in your case?

Otherwise, not using LINQ to SQL, maybe use SqlDataReader's NextResult to go through the results, for example:

IList<Employee> employees = new List<Employee>();
IList<Customer> customers = new List<Customer>();
using (SqlConnection connection = new SqlConnection
    (Properties.Settings.Default.NorthwindConnectionString))
using (SqlCommand command = new SqlCommand
    ("GetEmployeesAndCustomers", connection))
{
    command.CommandType = CommandType.StoredProcedure;
    connection.Open();
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            Employee e = new Employee{EmployeeID = (int)reader["EmployeeID"]};
            employees.Add(e);
        }
        reader.NextResult();
        while (reader.Read())
        {
            Customer c = new Customer{CustomerID = (string)reader["CustomerID"]};
            customers.Add(c);
        }
    }
}

Edit: Example of how to handle custom data combinations that are not easily fit into domain model objects; in this case retrieving orders along with the customers for the orders:

namespace Company.Application.ViewModel
{
    public class CustomerOrder
    {
        public string CustomerID { get; set; }
        public string CustomerName { get; set; }
        public int OrderID { get; set; }
        public DateTime? OrderDate { get; set; }
    }
}
namespace Company.Application.Repository
{
    public class CustomerOrderRepository
    {
        public IList<CustomerOrder> GetCustomerOrders()
        {
            NorthwindDataContext db = new NorthwindDataContext();
            var custorders = from customer in db.Customers
                             join order in db.Orders
                             on customer.CustomerID equals order.CustomerID
                             select new CustomerOrder
                             {
                                 CustomerID = customer.CustomerID,
                                 CustomerName = customer.CompanyName,
                                 OrderID = order.OrderID,
                                 OrderDate = order.OrderDate
                             };
            return custorders.ToList();
        }
    }
}

Inspiration for this: In the chapter about NerdDinner, Scott Guthrie talks about creating custom 'ViewModel' objects to hold data from for example joins that are not easily fit into the domain model objects.

Sign up to request clarification or add additional context in comments.

2 Comments

I had read that article, but it doesn't explain how to bring back multiple result sets in a stored procedure using LINQ. Additionally, it doesn't even explain how to use LINQ when bringing back custom sets of data, i.e. subsets or table combinations from queries using JOINs. I've yet to try your example out yet though.
Concerning bringing back custom sets of data, in the chapter here (aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf) about Nerddinner (weblogs.asp.net/scottgu/archive/2009/03/10/…), Scott Guthrie talks about creating custom 'ViewModel' objects to hold data from for example joins that are not easily fit into the domain model objects (I will give an example in the edited answer above)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.