-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathParent-Child-GrandChild-Query.linq
62 lines (51 loc) · 1.71 KB
/
Parent-Child-GrandChild-Query.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<Query Kind="Program">
<NuGetReference>Insight.Database</NuGetReference>
<Namespace>Insight.Database</Namespace>
<Namespace>System.Data.SqlClient</Namespace>
</Query>
//Parent-Child-GrandChild-Query
void Main()
{
var connection = new SqlConnection(MyExtensions.SQLConnectionString);
SqlInsightDbProvider.RegisterProvider();
var repo = connection.As<HelloRepo>();
var res1 = repo.GetParent();
res1.Dump();
var res2 = repo.GetParentChild();
res2.Dump();
}
public abstract class HelloRepo
{
public abstract IDbConnection GetConnection();
[Sql("SELECT 1 ParentId, 'Parent' Name, 'Last Parent' SecondName")]
public abstract Parent GetParent();
public IEnumerable<Parent> GetParentChild() =>
GetConnection().Query(@"
SELECT 1 ParentId, 'Parent' Name, 'Last Parent' SecondName
SELECT 1 ParentId, 4 ChildId, 'Child' ChildName, 'Last Child' SecondChildName
SELECT 4 ChildId, 6 GrandChildId, 'Grand Child' GranChildName, 'Grand Child Last' SecondGrandChildName",
Parameters.Empty, Query.Returns<Parent>()
.ThenChildren(Some<Child>.Records)
.ThenChildren(Some<GrandChild>.Records, parents: p => p.Childs, into: (p, c) => p.GrandChild = c),
CommandType.Text);
}
public class Parent
{
public int ParentId { get; set; }
public string Name { get; set; }
public string SecondName { get; set; }
public IEnumerable<Child> Childs { get; set; }
}
public class Child
{
public int ChildId { get; set; }
public string ChildName { get; set; }
public string SecondChildName { get; set; }
public IEnumerable<GrandChild> GrandChild { get; set; }
}
public class GrandChild
{
public int GrandChildId { get; set; }
public string GranChildName { get; set; }
public string SecondGrandChildName { get; set; }
}