3

I have one customer table and orders table with one-to-many relationship in database and my requirement is to get the corresponding list of orders for each customers.enter image description here

Here is the list of orders done for customerid = 1

I am able to do it by multiple cycle call of database(For example - first i collected the list of customers and then for each customers i collected their orders list in corresponding listDTO and finally returned the list of customer with oders DTO to the BAL Layer.

I think this is not good to call multiple time to call database to get the data. Is there any efficient way of doing it.

3
  • Use the JOIN, Luke. Commented Jan 22, 2016 at 6:37
  • You may post your some code here ... Otherwise in SQL you may do below ... You may add some Select C.*, O.* FROM Customers C Left Join Orders O on C.CustomerID = O.CustomerID Commented Jan 22, 2016 at 6:48
  • Thanks, join can be helpful but in case of one-to-many relationship i will get multiple records for each customers and after that i need to run loop to create Required DTO. am I correct? Commented Jan 22, 2016 at 8:11

2 Answers 2

0

While you can do a join, like you said you will get back multiple records for the each order duplicating customer information, which you code will have to de-duplicate.

I like to use SqlDataReader's support for multiple result sets to make this easier. https://msdn.microsoft.com/en-us/library/hh223698(v=vs.110).aspx

So you SQL will look something like this:

--Assume that @CustomerId is a parameter (always use parameters!)
SELECT CustomerId, CustomerName
  FROM Customer
 WHERE CustomerId = @CustomerId

SELECT OrderId, OrderDate --all your order columns
  FROM Orders
 WHERE CustomerId = @CustomerId

Then in your ADO.Net code:

using (var reader = command.ExecuteDataReader())
{
    while (reader.Read())
    {
        //read first result set
    }

    reader.NextResult();

    while (reader.Read())
    {
        //read secondresult set
    }
}

Or even better, use the async versions of ExecuteDataReader, Read, NextResult :-)

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

1 Comment

Thanks, @Cleverguy25. It was very helpful.
0

If you are using SQL 2016 this can be done very nicely with JSON path query to return a hierarchical structure of JSON which can then be deserialized into your objects, or possibly passed through to the client.

SELECT
            O.ORDER_ID orderId,
            O.ORDER_NAME orderName,
            O.ACTIVE_START orderDate,
            (SELECT
                OI.ORDER_ITEM_ID itemId,
                OI.NAME name,
                OI.COMMENTS comments
            FROM
                dbo.ORDER_ITEM OI
            WHERE
                O.ORDER_ID = OI.ORDER_ID
            FOR JSON PATH) items
        FROM
            dbo.[ORDER] O
        WHERE
            O.PERSON_ID = @PERSON_ID
        FOR JSON PATH;

Which would return something like

[
  {
    "orderId": 21,
    "orderName": "my first order",
    "orderDate": "2016-09-18T11:01:41",
    "items": [
      {
        "itemId": 41,
        "name": "pizza",
        "comments": "Extra Cheese Please"
      },
      {
        "itemId": 42,
        "name": "italian sandwich",
        "comments": "No peppers"
      }
    ]
  }
]...

There is a git project that has wrappers around ado for facilitating these kinds of calls at https://github.com/ahhsoftware/EzAdoVSSolution which might be something you'd be interested in.

Comments

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.