Getting a multidimensional result from a stored procedure with LINQ to SQL

I use LINQ to SQL to call a stored procedure with a single result set (I found a lot of information about processing multiple result sets, but this is not what I am doing). The result set is basically all the columns from the three tables joined together, and I have LINQ to SQL entities for these three tables. The scheme looks something like this:

  • Customer 1
    • Order 1
      • Product 1
      • Product 2
      • Product 3
    • Order 2
      • Product 4
  • Client 2
    • Order 3
      • Product 5
    • Order 4
      • Product 6
      • Product 7

, 1 , 1 . , , Order Customer. 7 .

, : 2 Customer LINQ to SQL, , Order, , ?

, - (dc - LINQ to SQL DataContext)...

var options = new DataLoadOptions();
options.LoadWith<Customer>(c => c.Orders);
dc.LoadOptions = options;

var customers = from c in dc.Customers select c;

... SQL, , SQL, , , Customer . , , .

, 7 Customer (5 ), . Orders Customer, , . Product, 7 , Order lazy , .

IMultipleResults, GetResult <Customer> () GetResult <Order> () GetResult <Product> () . , , GetResult < gt() - ( , GetResult <()). GetResult <() null.

, . , - , LINQ to SQL API ( LoadsWith ).

+3
1

, , :

  • sproc, (, ).
  • IMultipleResults ( ) LINQ to SQL, 3 IEnumerables
  • - , .. var groupedOrders = allReturnedOrders.GroupBy(order => order.CustomerID) var groupedProducts = allReturnedProducts.GroupBy(p => p.OrderID)
  • EntitySet.SetSource() , .. customer.Orders.SetSource(groupedOrders.Single(orderGroup => orderGroup.Key == customer.CustomerID)

DBML , (2-3 , ). sproc, , , ( Customer Order ).

0

Source: https://habr.com/ru/post/1710088/


All Articles