Dapper 一对多查询 one to many

原文:Dapper 一对多查询 one to many

参考文档:Dapper one to many

Table

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Born { get; set; }
    public Country Residience { get; set; }
    public ICollection<Book> Books { get; set; }
}

public class Country
{
    public int CountryId { get; set; }
    public string CountryName { get; set; }
}

public class Book
{
    public int BookId { get; set; }
    public string BookName { get; set; }
}

C# Code

pulic List<Person> GetPersons(){
 var sql = @"SELECT 1 AS Id, 'Daniel Dennett' AS Name, 1942 AS Born, 1 AS CountryId, 'United States of America' AS CountryName, 1 AS BookId, 'Brainstorms' AS BookName
UNION ALL SELECT 1 AS Id, 'Daniel Dennett' AS Name, 1942 AS Born, 1 AS CountryId, 'United States of America' AS CountryName, 2 AS BookId, 'Elbow Room' AS BookName
UNION ALL SELECT 2 AS Id, 'Sam Harris' AS Name, 1967 AS Born, 1 AS CountryId,  'United States of America' AS CountryName, 3 AS BookId, 'The Moral Landscape' AS BookName
UNION ALL SELECT 2 AS Id, 'Sam Harris' AS Name, 1967 AS Born, 1 AS CountryId,  'United States of America' AS CountryName, 4 AS BookId, 'Waking Up: A Guide to Spirituality Without Religion' AS BookName
UNION ALL SELECT 3 AS Id, 'Richard Dawkins' AS Name, 1941 AS Born, 2 AS CountryId,  'United Kingdom' AS CountryName, 5 AS BookId, 'The Magic of Reality: How We Know What`s Really True' AS BookName
UNION ALL SELECT 3 AS Id, 'Richard Dawkins' AS Name, 1941 AS Born, 2 AS CountryId,  'United Kingdom' AS CountryName, 6 AS BookId, 'An Appetite for Wonder: The Making of a Scientist' AS BookName";

var remainingHorsemen = new Dictionary<int, Person>();
connection.Query<Person, Country, Book, Person>(sql, (person, country, book) => {
    //person
    Person personEntity;
    //trip
    if (!remainingHorsemen.TryGetValue(person.Id, out personEntity))
    {
        remainingHorsemen.Add(person.Id, personEntity = person);
    }

    //country
    if(personEntity.Residience == null)
    {
        if (country == null)
        {
            country = new Country { CountryName = "" };
        }
        personEntity.Residience = country;
    }                    

    //books
    if(personEntity.Books == null)
    {
        personEntity.Books = new List<Book>();
    }

    if (book != null)
    {
        if (!personEntity.Books.Any(x => x.BookId == book.BookId))
        {
            personEntity.Books.Add(book);
        }
    }

    return personEntity;
},
splitOn: "CountryId,BookId");

return remainingHorsemen.Value.ToList();
}

原文地址:https://www.cnblogs.com/lonelyxmas/p/11572082.html

时间: 2024-10-17 07:37:45

Dapper 一对多查询 one to many的相关文章

三层架构的一点理解以及Dapper一对多查询

1.首先说一下自己对三层架构的一点理解 论坛里经常说会出现喜欢面相对象的写法,所以使用EF的,我个人觉得他俩没啥关系,先别反对,先听听我怎么说吧. 三层架构,基本都快说烂了,但今天还是说三层架构:UI,BLL,DAL.之前往往强调的是三层架构各司其职,但没有说三层架构中每层之间怎么交互的,以及人员之间的分工合作问题.今天重点说这个,从而回答面向对象和EF的关系. 今天说的与数据库有关,那就先说BLL和DAL之间数据的传递.有不少都是按照以下两种方式写的: (1)以Datatable传递.可想想,

mybatis0205 一对多查询 复杂

查询所有用户信息,关联查询订单及订单明细信息及商品信息,订单明细信息中关联查询商品信息 1.1sql 主查询表:用户信息 关联查询:订单.订单明细,商品信息 SELECT orders.*, user.username, user.sex , orderdetail.id orderdetail_id, orderdetail.items_num, orderdetail.items_id, items.name items_name, items.detail items_detail FRO

mybatis0204 一对多查询

查询所有订单信息及订单下的订单明细信息. sql语句 主查询表:订单表 关联查询表:订单明细 SELECT orders.*, user.username, user.sex , orderdetail.id orderdetail_id, orderdetail.items_num, orderdetail.items_id FROM orders, USER, orderdetail WHERE orders.user_id = user.id AND orders.id = orderde

mybatis学习笔记(11)-一对多查询

mybatis学习笔记(11)-一对多查询 mybatis学习笔记11-一对多查询 示例 小结 本文实现一对多查询,查询订单及订单明细的信息 示例 sql 确定主查询表:订单表 确定关联查询表:订单明细表 在一对一查询基础上添加订单明细表关联即可. SELECT orders.*, user.username, user.sex, user.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.item

Mybatis最入门---ResultMaps实例篇(一对多查询)

[一步是咫尺,一步即天涯] 接上文,我们来演示在实际开发中,如何配置和使用resultMap实现一对多查询. 准备工作: a.操作系统 :win7 x64 b.基本软件:MySQL,Mybatis,Spring,SQLyog,Tomcat,web基础 特别的,作为演示程序,还请各位看官不要纠结数据库的细节内容 ----------------------------------------------------------------------------------------------

Hibernate(二)——一对多查询

1. 前言 本章节我们讨论Hibernate一对多查询的处理. 在上一章节中(Hibernate(一)——入门),我们探讨了Hibernate执行最基本的增删改查操作.现在我们将情况复杂化:加入我们在查询用户信息的时候需要同时查询其登录日志,这样就涉及到一对多查询.那么一对多查询要怎么实现么? 2. jar包准备 在本节中,除了上一章节中用到的jar包,我还需要用log4j.jar来将Hibernate的查询语句输出到控制台.log4j.properties的配置如下: 1 log4j.root

Dapper 链式查询 扩展

Dapper 链式查询扩展 DapperSqlMaker   Github地址:https://github.com/mumumutou/DapperSqlMaker Demo: 查询       TestsDapperSqlMaker\DapperSqlMaker.Test\ SelectDapperSqlMakerTest.cs 添加       TestsDapperSqlMaker\DapperSqlMaker.Test\  InsertDapperSqlMakerTest.cs 更新 

Excel一对多查询(index+small+if)

一.学习 一对多查询模式化数组公式: =INDEX(区域,SMALL(IF(条件,行号数组,4^8),ROW(A1))) 三键齐按(ctrl+shift+回车) 在具有多个符合条件的情况下,提取和匹配方法~ 我们需要解决三种类型的问题: 1,李商隐第一次出现时的B列值: 2,李商隐最后一次出现时的B列值: 3,李商隐第n次出现时候的B列值(假设n=2). 前面两个问题是比较常见的,我们简要一看. 匹配第一个,可以直接用: =VLOOKUP(E2,A2:B17,2,FALSE) 匹配最后一个,可以

非关心数据库无法进行连表查询 所以我们需要在进行一对多查询时候 无法满足 因此需要在&quot;1&quot;的一方添加&quot;多&quot;的一方的的id 以便用于进行连表查询 ; 核心思想通过id进行维护与建文件

非关心数据库无法进行连表查询 所以我们需要在进行一对多查询时候 无法满足 因此需要在"1"的一方添加"多"的一方的的id 以便用于进行连表查询 ; 核心思想通过id进行维护与建文件 原文地址:https://www.cnblogs.com/classmethond/p/10562396.html