• Dapper.Net简例


    Dapper的多表查询实现

    打开链接
    using (var conn = new SqlConnection(myConnectionString)) {
        conn.Open();
        ....
    }
    

    单表查询
    public class Account {
      public int? Id {get;set;}
      public string Name {get;set;}
      public string Address {get;set;}
      public string Country {get;set;}
      public int ShopId {get; set;}
    }
    返回列表
    IEnumerable<Account> resultList = conn.Query<Account>(@"
                        SELECT * 
                        FROM Account
                        WHERE shopId = @ShopId", 
    new {  ShopId = shopId });

    返回单个对象
    Account result = conn.Query<Account>(@"
                        SELECT * 
                        FROM Account
                        WHERE Id = @Id", 
       new {  Id = Id }).FirstOrDefault();
    

     返回dynamic

    dynamic account = conn.Query<dynamic>(@"
                        SELECT Name, Address, Country
                        FROM Account
    		    WHERE Id = @Id", new { Id = Id }).FirstOrDefault();
    

     

    嵌有单个对象

    public class Account {
      public int? Id {get;set;}
      public string Name {get;set;}
      public string Address {get;set;}
      public string Country {get;set;}
      public int ShopId {get; set;}
      public Shop Shop {get;set;}
    }
    public class Shop {
      public int? ShopId {get;set;}
      public string Name {get;set;}
      public string Url {get;set;}
    }
    

     查询

    var resultList = conn.Query<Account, Shop, Account>(@"
                        SELECT a.Name, a.Address, a.Country, a.ShopId
                                s.ShopId, s.Name, s.Url
                        FROM Account a
                        INNER JOIN Shop s ON s.ShopId = a.ShopId                    
                        ", (a, s) => {
                             a.Shop = s;
                             return a;
                         },
                         splitOn: "ShopId"
                         ).AsQueryable();
    

    嵌有列表对象

    public class Shop {
      public int? Id {get;set;}
      public string Name {get;set;}
      public string Url {get;set;}
      public int ShopId {get;set;}
      public IList<Account> Accounts {get;set;}
    }
    
    public class Account {
      public int? Id {get;set;}
      public string Name {get;set;}
      public string Address {get;set;}
      public string Country {get;set;}
      public int ShopId {get;set;}
    }
    

      

    var lookup = new Dictionary<int, Shop>()
    conn.Query<Shop, Account, Shop>(@"
                        SELECT s.*, a.*
                        FROM Shop s
                        INNER JOIN Account a ON s.ShopId = a.ShopId                    
                        ", (s, a) => {
                             Shop shop;
                             if (!lookup.TryGetValue(s.Id, out shop)) {
                                 lookup.Add(s.Id, shop = s);
                             }
                             if (shop.Accounts == null) 
                                 shop.Accounts = new List<Account>();
                             shop.Accounts.Add(a);
                             return shop;
                         },
                         ).AsQueryable();
    
    var resultList = lookup.Values;
    

      





  • 相关阅读:
    XTREE随笔
    多重共线性
    常用特征选取算法
    最短路径算法的实现(dijskstra):Python
    数据科学的完整学习路径—Python版(转载)
    windows下64位python的安装及机器学习相关包的安装(实用)
    拓扑排序 详解 + 并查集 详解 + 最小生成树(MST)详解 【普利姆算法 + 优先队列优化 & 克鲁斯卡尔算法】
    最短路算法 :Bellman-ford算法 & Dijkstra算法 & floyd算法 & SPFA算法 详解
    在linux下部署项目所用到的基本linux命令
    素数筛 模板
  • 原文地址:https://www.cnblogs.com/chengNet/p/14745763.html
Copyright © 2020-2023  润新知