Dapper Many-to-many query objects and object lists

Source: Internet
Author: User


splitOn参数
lookup
注意:lookup中添加的是临时定义的Group对象,并在循环中将此对象从lookup中取出,为其RightsList添加Rights


class Rights{    public string ID{get;set;}    public string Name{get;set;}}









class Group{ public string ID{get;set;} public string Name{get;set;} public List<Rights> RightsList{get;set;}}







CREATETABLE' T_group_right ' (' Rightid 'varchar50)NotNull' GroupId 'varchar50)NotNullKEY' Fk_group_rights_id ' (' Rightid '), KEY ' fk_rights_group_id ' (' groupId '), CONSTRAINT ' fk_group_rights_id ' FOREIGN KEY (' Rightid ') REFERENCES ' t_rights ' (' id '), CONSTRAINT ' fk_rights_group_id ' FOREIGN KEY (' groupId ') REFERENCES ' t_group ' (' id ')) engine=innodb DEFAULT charset= UTF8;



Query Role List

Public list<group> GetAll () {String sql ="Select a.*,c.* from T_group a left joins T_group_right b on a.id = B.groupid left joins t_rights C on C.id=b.rightid"; using (Mysqlconnection conn = Dapperfactory.cratemysqlconnection ()) {var lookup = new dictionary<string, group> (); var list = conn. Query<group, rights, group>(SQL, (g, r) = {Group tmp; if (!lookup. TryGetValue (g.id, out tmp)) {tmp = g; lookup. ADD (G.id, TMP); } tmp. Rightslist.add (R); return G;} , SplitOn: "id"). ToList(); return to lookup. Values. ToList();}}




Operator对象
class Operator{    public string ID{get;set;}    public string Name{get;set;} [NoWrite] public List<Group> GroupList { get; set; } [NoWrite] public List<Rights> RightsList { get; set; }}



查询一个Operator对象
Public Operator Get (string id) {using (Mysqlconnection conn = Dapperfactory.cratemysqlconnection ()) {String sql ="Select a.*,c.*,e.* from T_operator a left joins T_operator_group b on a.id = B.operatorid left joins T_group C on c.id=b.gr Oupid "+"left joins t_operator_rights D on a.id = D.operatorid left joins t_rights e on e.id = d.rightid where [email protected]                ";                Operator lookup = null; Conn. Query<operator, Group, rights, operator>(SQL, (O, g, r) = = { if (lookup = = null) {lookup = O;} Group tmp = lookup. Grouplist.find (f = f.id = = g.id); if (tmp = = null) {TMP = g; lookup. Grouplist.add (TMP); } tmp. Rightslist.add (R); Lookup. Rightslist.add (R); return o;}, param: new {id = id}, SplitOn: "id"); return Lookup;}}           













Get operator list:


Public list<operator> GetAll () {using (Mysqlconnection conn = Dapperfactory. Cratemysqlconnection ()) {String sql ="Select a.*,c.*,e.* from T_operator a left joins T_operator_group b on a.id = B.operatorid left joins T_group C on c.id=b.gr Oupid "+"Left joins T_operator_rights D on a.id = D.operatorid left joins t_rights e on e.id = D.rightid"; var lookup = new Dictionary<string,operator> (); Conn. Query<operator, Group, Rights, operator> (SQL, (O, g, r) = = {Operator tmp; if (!lookup. TryGetValue (o. ID,Out tmp)) {TMP = O; Lookup. ADD (o. id,tmp); } Group Tmpg = tmp. Grouplist. Find (f = f. ID = = G. ID); if (Tmpg = = null) {Tmpg = g; Tmp. Grouplist. ADD (Tmpg); }//Role permissions list rights TMPR = Tmpg. Rightslist. Find (f = f.id = = R.id) ; if (TmpR = = null) {Tmpg.id = = R.id) "id" )

Dapper Many-to-many query objects and object lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.