Dapper學習 - Dapper的基本用法(一)

來源:互聯網
上載者:User

標籤:不同   order   一對一   select   欄位   返回   contact   left join   ret   

上一篇, 提到Query<Test>查詢的時候, 如果Test中包含自訂class, Dapper不會給自訂class完成映射, 而是直接給null, 其實是可以實現的, 答案就在下面的基本用法介紹中

 提到用法, 首先是要實現CURD, 這裡先介紹Read吧.

先上實體:

    public enum Gender    {         男 = 0,        女    }    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 Contact { get; set; }        public Tch_Teacher Teacher { get; set; }    }

一、一對一映射

sql = "select Sex, Id, Name, CreateDate, No from tch_teacher limit 3;"; var list = conn.Query<Tch_Teacher>(sql);Console.WriteLine(list.ToList().FirstOrDefault().Name);  

一對一映射還是蠻簡單的, 不需要解釋了,  這種寫法, 就是前面提到的, 如果Tch_Teacher裡面有一個Tch_Contact類型的屬性, 那麼在映射的時候, 這個屬性會直接賦值為null

二、一對多映射

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這個欄位, 在類中並沒有var list1 = conn.Query<Test, Tch_Contact, TeacherInfo>(sql, (a, b) =>{ TeacherInfo tInfo = new TeacherInfo(); if (a != null) { tInfo.Teacher = a; } if (b != null) { tInfo.Contact = b; } return tInfo;});//}, null, null, true, "Id", null, null);

此時, 返回的 List<TeacherInfo> 中, Teacher 和 Contact 都是有值的, 而不是直接給null, 當然, TeacherInfo中, 還可以有別的欄位, 比如 public int No {get;set;},

此時的No是能擷取到值的. 其中的映射原理, 這裡我就不再贅述了, 能看懂Dapper中一對一映射的原理, 一對多映射也就差不多了. 

 如果將 TeacherInfo類中Contact屬性修改一下, 改成 List<Tch_Contact>類型, 那麼怎樣得到一個按 Tch_Teacher 分組的資料呢?

通常情況下, 會想到兩種辦法:

一種是上面這種辦法, 先得到全部資料, 然後通過GroupBy方法, 進行分組, 也能得到那種分組資料, 但那並不理想, 這種方法我就不貼了.

  1. 得到的資料, 並不是我們想要的那種格式資料, 雖然也是分組的資料.

  2. 有重複項. Tch_Teacher並沒有得到去除重複的功能. 雖然分了組, 但是每組中的資料, Tch_Teacher仍然是重複的.

 另一種, 就是通過一個中間變數來擷取, 方法如下:

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];});

使用字典項, 在其中進行一個規整操作, 去除重複的 Tch_Teacher資訊, 把Tch_Contact進行規整分組. 而且, 得到的結果, 就直接是想要的結果

public class TeacherInfo{    public List<Tch_Contact> Contact { get; set; }    public Tch_Teacher Teacher { get; set; }}

 話說回來, 至於最終使用哪種方法, 要根據具體使用需求來定, 總體來說, 都能達到效果. 方法無好壞, 只是使用情境不同. 

Dapper學習 - Dapper的基本用法(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.