using System.Collections.Generic;
public static void Main()
DataTable dt = new DataTable();
dt.Columns.Add("CustomerId", typeof(int));
dt.Columns.Add("CustomerName");
dt.Columns.Add("ProductId", typeof(int));
dt.Columns.Add("ProductName");
dt.Columns.Add("Quantity", typeof(int));
dt.Rows.Add(10, "Acme Inc.", "Albuquerque, NM", 100, "Anvil", 5);
dt.Rows.Add(10, "Acme Inc.", "Albuquerque, NM", 101, "Jet Suit", 1);
dt.Rows.Add(10, "Acme Inc.", "Albuquerque, NM", 102, "Roller Skates, 1 Pair", 1);
dt.Rows.Add(20, "Cyberdyne Systems", "Sunnyvale, CA", 202, "T-800", 6);
dt.Rows.Add(20, "Cyberdyne Systems", "Sunnyvale, CA", 203, "T-1000", 12);
var reader = new MockSqlDataReader(dt);
var customersById = new Dictionary<int, Customer>();
int customerId = (int)reader["CustomerId"];
if (!customersById.TryGetValue(customerId, out customer))
Name = (string)reader["CustomerName"],
City = (string)reader["City"],
Products = new List<Product>()
customersById.Add(customerId, customer);
Product product = new Product
ProductId = (int)reader["ProductId"],
Name = (string)reader["ProductName"],
Quantity = (int)reader["Quantity"]
customer.Products.Add(product);
Console.WriteLine("Product list by customer:\n");
foreach (Customer cust in customersById.Values)
Console.WriteLine(string.Format("{0}) {1} of {2}", cust.CustomerId, cust.Name, cust.City));
foreach (Product prod in cust.Products)
Console.WriteLine(string.Format("\t{0}) {1} (qty {2})", prod.ProductId, prod.Name, prod.Quantity));
public int CustomerId { get; set; }
public string Name { get; set; }
public string City { get; set; }
public List<Product> Products { get; set; }
public int CustomerId { get; set; }
public int ProductId { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public MockSqlDataReader(DataTable table)
return rowIndex < table.Rows.Count;
public object this[string colName]
get { return table.Rows[rowIndex][colName]; }