Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You use <a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.nextresult(VS.80).aspx" rel="noreferrer">the NextResult</a> method on the datareader to navigate with multiple results from a query.</p> <p>To loop through all data you would do something like this:</p> <pre><code>var moreResults = true; while (moreResults) { while (reader.Read()) { ... } moreResults = reader.NextResult(); } </code></pre> <p>So with that as a background, and assuming the master resultset comes first, populating master and detail objects could be done like this:</p> <p>First, build up a dictionary of the Master records:</p> <pre><code>var masters = new Dictionary&lt;int, Master&gt;(); var idOrdinal = reader.GetOrdinal("id"); while (reader.Read()) { var id = reader.GetInt32(idOrdinal); masters.Add(id, new Master{Id=id, ....}); } </code></pre> <p>Next, move on to detail records and add those to their corresponding master:</p> <pre><code>reader.NextResult(); var masterIdOrdinal = reader.GetOrdinal("masterId"); while (reader.Read()) { var masterId = reader.GetInt32(masterIdOrdinal); var master = masters[masterId]; master.Details.Add(new Detail{....}); } </code></pre> <p>You should obviously replace column names with what you have in your data as well as supply the full initialization of Master and Detail objects. If the detail resultset is sorted on master id, the last loop could be optimized to only lookup each master once from the dictionary. If the resultsets are small though, the gain would not be that huge.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload