DapperDynamicResult

CSharp · Console

/Program.cs

using Dapper;
using System.Collections.Generic;
using System.Data.SqlClient;
using System;

namespace TesteDapper
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var conexao = @"Server=.\sqlexpress;Database=StackOverFlow;Trusted_Connection=True;";

            using (var conn = new SqlConnection(conexao))
            {
                conn.Open();
                var sql = @"SELECT * 
                             FROM CLIENTE CLI
                             LEFT JOIN TELEFONE TEL 
                               ON TEL.IdCliente = CLI.IdCliente";

                dynamic resultList = conn.Query(sql);
				
				foreach(var r in resultList)
				{
					Console.WriteLine(r);
				}
            }
        }
    }

    public class Cliente
    {
        public int IdCliente { get; set; }
        public string Nome { get; set; }
        public List<Telefone> Telefones { get; set; }
    }

    public class Telefone
    {
        public int IdTelefone { get; set; }
        public int IdCliente { get; set; }
        public string Numero { get; set; }
    }
	
	
	///
	/// Fake class
	///
	public class SqlConnection : IDisposable
	{
		
		public SqlConnection(string conexao)
		{
			
		}
		
		public void Open()
		{
			
		}
		
		public dynamic Query(string query)
		{
			var c1 = new { IdCliente = 1, Nome = "Joao", IdTelefone = 1, Numero = "12345678"};
			var c2 = new { IdCliente = 1, Nome = "Joao", IdTelefone = 2, Numero = "98765432"};
			var c3 = new { IdCliente = 2, Nome = "Maria", IdTelefone = 3, Numero = "55555555"};
			var c4 = new { IdCliente = 3, Nome = "Jose", IdTelefone = 4, Numero = "77777777"};
			return new[] { c1, c2, c3, c4 };
		}
		
		public void Dispose()
        {
			
        }
	}
}
;