zoukankan      html  css  js  c++  java
  • Dapper关联查询

    1、一对一:

      using (IDbConnection connecton = new MySqlConnection(ConfigurationManager.ConnectionStrings["default"].ConnectionString))
                {
                    string sql = @"SELECT *
                                   from expand_userbasicinformation b
                                   JOIN expand_users u on b.UserId = u.UserId";
                    var infos = connecton.Query<UserInfo, User, UserInfo>(sql, (info, user) =>
                    {
                        info.User = user;
                        return info;
                    }, splitOn: "UserId");
                }

    其中,实体定义如下:

      public class UserInfo
        {
            public string InformationId { get; set; }
    
            public string UserId { get; set; }
    
            public User User { get; set; }
    
            public string RealName { get; set; }
        }
    
        public class User
        {
            public string UserId { get; set; }
    
            public string LoginName { get; set; }
    
            public string LoginPassword { get; set; }
        }

    2、一对多:

    using (IDbConnection connecton = new MySqlConnection(ConfigurationManager.ConnectionStrings["crmcontroller"].ConnectionString))
                {
                    List<Client> clients = new List<Client>();
                    string sql = @"SELECT *
                                   from crm_client_list
                                   join crm_client_field_value on ClientId = ModelId";
                    var infos = connecton.Query<Client, ClientField, Client>(sql, (c, f) =>
                    {
                        var currentClient = clients.Find(x => x.ClientId == c.ClientId);
                        if (currentClient == null)
                        {
                            c.Fields.Add(f);
                            clients.Add(c);
                            return c;
                        }
                        else
                        {
                            currentClient.Fields.Add(f);
                            return currentClient;
                        }
                    }, splitOn: "ModelId");
                }

    其中,实体定义如下:

      public class Client
        {
            public Client()
            {
                this.Fields = new List<ClientField>();
            }
    
            public string ClientId { get; set; }
    
            public string Name { get; set; }
    
            public string PhoneNumber { get; set; }
    
            public string IDNumber { get; set; }
    
            public List<ClientField> Fields { get; private set; }
        }
    
        public class ClientField
        {
            public string FieldValueId { get; set; }
    
            public string ModelId { get; set; }
    
            public string FieldId { get; set; }
    
            public string FieldValue { get; set; }
        }
  • 相关阅读:
    PowerDesigner快捷键
    Android 的开源电话/通讯/IM聊天项目全集
    Android ContentProvider完整案例
    Android中观察者模式的升入理解
    Android中Socket大文件断点上传
    Storm概念学习系列之Tuple元组(数据载体)
    开始使用storm
    Storm概念学习系列之storm的功能和三大应用
    Storm概念学习系列之storm的特性
    Storm概念学习系列之storm核心组件
  • 原文地址:https://www.cnblogs.com/guokun/p/5843871.html
Copyright © 2011-2022 走看看