For a very simple example, A Company has man users. I have two tables, Company and User. The User table has a foreign key CompanyId. Say I have to fill a list of Companies and all their Users, how would I map this with a datareader?
Do I need multiple select statements? One to get all companies needed and then another to get all users within the selected companies? Pseudo code below.
select companies;
Loop through all companies returned and create a list of companyids for where in.
select users where in (companyid list created above)
attach users to selected companies
Logically, what would be the best approach to accomplish this task.
If you don’t want to use an ORM (which would make this much easier), then you want to do a JOIN statement, and then perhaps use a
Dictionaryto store the primary key of each company as the key of the dictionary, and then aCompanyinstance as the value for the dictionary, which will contain a collection ofUserinstances.E.g.