Join 3 lists in C # from queries in the database [closed]

1

I'm making a page to generate a report. In this I make 3 different selects , placing the result of each query in a list:

public IEnumerable<RelatorioVencerVencidos> ObterRelatorioVencerVencidos(ParametrosVencerVencidos filtro)
    {

        var hash = new Dictionary<int, RelatorioVencerVencidos>();

        CarregarListVencerVencidos(hash,filtro);
        CombinarComDadosDeRendaFixa(hash);
        CombinarComDadosDeClubes(hash);

        return hash.Values.ToArray();
    }

    private void CombinarComDadosDeClubes(Dictionary<int, RelatorioVencerVencidos> hash)
    {
        foreach (var clubes in Instance.ObterClubesVirtual())
        {
            if (hash.ContainsKey(clubes.CodCotista))
                hash[clubes.CodCotista].Clubes = clubes.SaldoBruto;
        }
    }

    private void CombinarComDadosDeRendaFixa(Dictionary<int, RelatorioVencerVencidos> hash)
    {
        foreach (var renda in Instance.ObterRendaFixaVirtual())
        {
            var codCliente = default(int);
            if (int.TryParse(renda.CodCliente, out codCliente))
                if (hash.ContainsKey(codCliente))
                    hash[codCliente].RendaFixa = renda.ValorBruto;
        }
    }

    public void CarregarListVencerVencidos(Dictionary<int, RelatorioVencerVencidos> hash, ParametrosVencerVencidos filtros)
    {

        foreach (var i in Instance.ObterDadosVencerVencidos(filtro))
        {
            if (!hash.ContainsKey(i.CodigoBovespa))
                hash.Add(i.CodigoBovespa, i);
        }
    } 

I'm editing the question, because I've done it the way above, using Dictionary, where I load the lists into a dictionary using the client code as the key and get the client's balance as a dictionary value. Only one thing that is not working very well is due to my method GetDataVencerVencidos that is in the last foreach that receives some parameters and the parameters end up being zeroed being that in the method GetReportVencerVencidos the Parametros are with values, however when it enters the method LoadListVencer the parameters they are null and I am not able to know the correct way to get the parameter information in the method GetDataVencerVencidos.

    
asked by anonymous 08.12.2016 / 12:06

1 answer

3

I think the question is related to object-oriented concepts. It happens that your "join" variable does not have the same type of "list-clouds" and "list-lists". The join statement should be:

List<IDadosVencerVencidos> join = new List<IDadosVencerVencidos>(listaRendaFixa.Count + listaClubes.Count + listaRelatorio.Count);

This example I did works correctly:

Create the interface

interface IDados
{
    int Codigo { get; set; }
    String  Descricao { get; set; }
}
class Cliente : IDados
{
    public int Codigo { get; set; }
    public string Descricao { get; set; }
    public DateTime Aniversario { get; set; }
}
class Fornecedor : IDados
{
    public int Codigo { get; set; }
    public string Descricao { get; set; }
    public string Endereco { get; set; }
}
    static void Main(string[] args)
    {
        List<Cliente> lstCliente = new List<Cliente>();
        lstCliente.Add(new Cliente { Codigo = 1, Descricao = "C1", Aniversario = DateTime.Now });
        lstCliente.Add(new Cliente { Codigo = 2, Descricao = "C2", Aniversario = DateTime.Now });
        lstCliente.Add(new Cliente { Codigo = 3, Descricao = "C3", Aniversario = DateTime.Now });


        List<Fornecedor> lstFornecedor = new List<Fornecedor>();
        lstFornecedor.Add(new Fornecedor { Codigo = 100, Descricao = "F1", Endereco = "End. 1" });
        lstFornecedor.Add(new Fornecedor { Codigo = 200, Descricao = "F2", Endereco = "End. 2" });


        List<IDados> lstJoin = new List<IDados>(lstCliente.Count + lstFornecedor.Count);

        lstJoin.AddRange(lstCliente);
        lstJoin.AddRange(lstFornecedor);

        foreach (IDados d in lstJoin)
        {
            Console.WriteLine(String.Format("> {0} - {1}", d.Codigo, d.Descricao));
        }

        Console.ReadKey();
    }

The end result is a list containing 5 objects of type "IDados".

    
08.12.2016 / 12:55