In the previous article, referring to query<test> query, if the Test contains a custom class, dapper will not give the custom class to complete the mapping, but directly to NULL, in fact, can be achieved, the answer is in the following basic usage introduction
When it comes to usage, the first thing to do is to implement Curd, which first introduces read.
First on the entity:
Public enumGender {male=0, female} Public classTch_teacher { Public intId {Get;Set; } PublicGender Sex {Get;Set; } Public stringName {Get;Set; } Public BOOLisdoubleposition {Get;Set; } PublicDateTime CreateDate {Get;Set; } } Public classTch_contact { Public intId {Get;Set; } Public intTId {Get;Set; } Public stringPhone {Get;Set; } Public stringQQ {Get;Set; } Public stringWeixin {Get;Set; } } Public classTeacherinfo { PublicTch_contact Contact {Get;Set; } PublicTch_teacher Teacher {Get;Set; } }
I. Mapping
" Select Sex, Id, Name, CreateDate, No from Tch_teacher limit 3; " var list = conn. query<tch_teacher>(SQL); Console.WriteLine (list. ToList (). FirstOrDefault (). Name);
It's pretty simple to do one-to-one mapping, and it doesn't have to be explained, as mentioned earlier, if Tch_teacher has a tch_contact-type attribute in it, the property will be directly assigned null at the time of the mapping.
Two or one-to-many mappings
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 3;";//No This field, not in the classvarLIST1 = conn. Query<test, Tch_contact, teacherinfo> (SQL, (A, b) = ={teacherinfo Tinfo=NewTeacherinfo (); if(A! =NULL) {Tinfo.teacher=A; } if(b! =NULL) {tinfo.contact=b; } returntinfo;});//}, NULL, NULL, True, "Id", NULL, NULL);
At this point, the returned list<teacherinfo>, Teacher and contact are both valued, not directly null, and of course, in Teacherinfo, there can be other fields, such as public int No {get; Set;},
No is able to get the value at this time. One of the mapping principle, I will not repeat here, can read the principle of a one-to-one mapping in dapper, a one-to-many mapping is almost.
If you change the Contact property in the Teacherinfo class to the list<tch_contact> type, how do you get a data grouped by Tch_teacher?
In general, there are two ways of thinking:
One is the above approach, the first to get all the data, and then through the GroupBy method, grouping, but also to get that kind of grouping data, but that is not ideal, this method I do not post.
1. The data obtained is not the format data we want, although it is also a grouped data.
2. There are duplicates. Tch_teacher does not get the ability to remove duplicates. Although the group is divided, the data in each group, Tch_teacher, is still duplicated.
The other is obtained by means of an intermediate variable, 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_con Tact B on a.id = B.tid ORDER BY a.id ASC limit 6;"; varInfos =Newdictionary<int, teacherinfo>();varLIST1 = conn. Query<test, Tch_contact, teacherinfo> (SQL, (A, b) = ={Teacherinfo tinfo; if(!infos. TryGetValue (a.ID, outtinfo)) {Tinfo=NewTeacherinfo (); Tinfo.contact=NewList<tch_contact>(); Tinfo.teacher=A;infos. ADD (a.ID, tinfo); } if(b! =NULL) {Infos[a.id]. Contact.add (b); } returninfos[a.id];});
Using dictionary items, a structured operation is performed to remove duplicate tch_teacher information and to group Tch_contact. Moreover, the results obtained are directly the result of the desired
Public class teacherinfo{ publicgetset;} Public Get Set ; }}
In other words, the final use of which method, according to the specific use of the needs to be determined, overall, can achieve results. Methods are not good or bad, just use different scenes.
Dapper Study-basic usage of dapper (i.)