From b451cd5aef8543caac0b9c0514687b8323cd1942 Mon Sep 17 00:00:00 2001 From: mgravell Date: Mon, 6 Jun 2011 10:31:37 +0100 Subject: [PATCH] SO parent child example --- Tests/Tests.cs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Tests/Tests.cs b/Tests/Tests.cs index 25e42f2..5153c6c 100644 --- a/Tests/Tests.cs +++ b/Tests/Tests.cs @@ -686,7 +686,36 @@ public void TestDapperSetsPrivates() { connection.Query("select 'one' ShadowInDB").First().Shadow.IsEqualTo(1); } - + + class Parent + { + public int Id { get; set; } + public readonly List Children = new List(); + } + class Child + { + public int Id { get; set; } + } + public void ParentChildIdentityAssociations() + { + var lookup = new Dictionary(); + var parents = connection.Query(@"select 1 as [Id], 1 as [Id] union all select 1,2 union all select 2,3 union all select 1,4 union all select 3,5", + (parent, child) => + { + Parent found; + if (!lookup.TryGetValue(parent.Id, out found)) + { + lookup.Add(parent.Id, found = parent); + } + found.Children.Add(child); + return found; + }).Distinct().ToDictionary(p => p.Id); + parents.Count().IsEqualTo(3); + parents[1].Children.Select(c => c.Id).SequenceEqual(new[] { 1,2,4}).IsTrue(); + parents[2].Children.Select(c => c.Id).SequenceEqual(new[] { 3 }).IsTrue(); + parents[3].Children.Select(c => c.Id).SequenceEqual(new[] { 5 }).IsTrue(); + } + /* TODO: * public void TestMagicParam() -- GitLab