Dapper learning, dapper
In the previous article, when we talked about Query <Test>, if Test contains a custom class, Dapper does not map the custom class, but directly returns null. In fact, it can be implemented, the answer is in the basic usage introduction below.
When it comes to usage, You need to implement CURD first. Here we will introduce Read first.
First import the instance:
Public enum Gender {male = 0, female} public class Tch_Teacher {public int Id {get; set;} public Gender Sex {get; set;} public string Name {get; set;} public bool IsDoublePosition {get; set;} public DateTime CreateDate {get; set;} public class Tch_Contact {public int Id {get; set ;} public int TId {get; set;} public string Phone {get; set;} public string QQ {get; set;} public string Weixin {get; set ;}} public class TeacherInfo {public Tch_Contact {get; set;} public Tch_Teacher Teacher {get; set ;}}
1. One-to-one ing
var sql = string.Empty;sql = "select Count(1) from tch_teacher where id>@Id limit 3;";Console.WriteLine(conn.Query<int>(sql, new { Id = 10 }));Console.WriteLine(conn.Query<string>(sql, new { Id = 1 }));sql = "select Sex, Id, Name, CreateDate, No from tch_teacher limit 3;";var list = conn.Query<Test>(sql);
var list1 = conn.Query<object>(sql);
var list2 = conn.Query<dynamic>(sql);Console.WriteLine(list.ToList().FirstOrDefault().Name);
One-to-one ing is quite simple and does not need to be explained. This method is mentioned above. If Tch_Teacher has a Tch_Contact type attribute, during ing, this attribute is directly assigned null.
2. One-to-multiple ing
SQL = @ "select a. Id, a. Sex, a. Name, a. CreateDate, a. No, B. Id, B. Phone, B. QQ,
B. weixin from tch_teacher a left join tch_contact B on. id = B. TId order by. id asc limit 3; "; // No. The var list1 = conn is not found in the class. query <Test, Tch_Contact, TeacherInfo> (SQL, (a, B) =>{ TeacherInfo tInfo = new TeacherInfo (); if (! = Null) {tInfo. Teacher = a;} if (B! = Null) {tInfo. Contact = B;} return tInfo;}); //}, null, null, true, "Id", null, null );
In this case, in the returned List <TeacherInfo>, both Teacher and Contact have values, instead of directly giving null. Of course, other fields can also exist in TeacherInfo, such as public int No {get; set ;},
At this time, No can get the value. Here I will not go into details about the ing principle. I can understand the one-to-one ing principle in Dapper, and the one-to-many ing will be similar.
If you change the Contact attribute in the TeacherInfo class to the List <Tch_Contact> type, how can you get data grouped by Tch_Teacher?
Generally, two methods are used:
One way is to obtain all the data first, and then group the data through the GroupBy method. But that is not ideal. I will not post this method.
1. The obtained data is not in the format we want, although it is also grouped data.
2. There are repeated items. Tch_Teacher does not get the function of removing duplicates. Although it is divided into groups, Tch_Teacher is still repeated for data in each group.
Another method is to obtain it through an intermediate variable. The method is as follows:
sql = @"select a.Id, a.Sex, a.Name, a.CreateDate, a.No, b.Id, b.Phone, b.QQ, b.Weixin from tch_teacher a left join tch_contact b on a.Id = b.TId order by a.Id asc limit 6;"; var infos = new Dictionary<int, TeacherInfo>();var list1 = conn.Query<Test, Tch_Contact, TeacherInfo>(sql, (a, b) =>{ TeacherInfo tInfo; if (!infos.TryGetValue(a.Id, out tInfo)) { tInfo = new TeacherInfo(); tInfo.Contact = new List<Tch_Contact>(); tInfo.Teacher = a; infos.Add(a.Id, tInfo); } if (b != null) { infos[a.Id].Contact.Add(b); } return infos[a.Id];});
Use a dictionary item to perform a normalization operation in it, remove duplicate Tch_Teacher information, and group Tch_Contact. In addition, the expected result is directly the expected result.
public class TeacherInfo{ public List<Tch_Contact> Contact { get; set; } public Tch_Teacher Teacher { get; set; }}
In other words, the final method should be determined based on the specific needs. In general, the results can be achieved. The method is not good or bad, but the Use scenario is different.