Make Join of 3 lists in c# from queries in the database

Asked

Viewed 114 times

1

I am making a page to generate a report. In this I make 3 selects different, putting 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 am editing the question, due to I have done as above, using Dictionary, where I load the lists into a Dictionary using the account code as the key and take the account balance as Dictionary value. Only one thing that is not working very well is due to my method Obtainingswinning that this in the last foreach that receives some parameters and the parameters end up coming zeroed being that in the Get methodwinning method the Parameters are with values, however when you enter the Load methodwinning the parameters are null and I am not able to know the correct way to get the information of the parameters in the Obtainingwinning method.

  • And what’s the mistake?

  • The error is this one below in the two lines that I highlighted as an error in the question: The best Overload method match for 'System.Colections.Generic.list<Reportwinexpired>. Addrange(System.collections. Generic.Ienumerable<Reportwinnowers>)' has some invalid Arguments

1 answer

3


I think the question is related to object orientation concepts. It turns out that its variable "Join" does not have the same type of "list clubs" and "list reports". 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 of 5 objects of type "Idados".

  • Thanks for the help, I declared using my interface, but at the time of Return Join, was still giving conversion error.

  • You can edit your question and add the new code, preferably with the implementation of the classes?

  • Edited Question Edited Tomas. You can help ?

  • What type of object does the Instance method.Obtainsmatterings return? Add the structure of this object to the question (with methods and properties), then I think we can solve the problem. As for the fact of null parameters, it is also necessary to know which type of the filter variable, where it is being declared and where it is being changed. It is not possible to infer anything from the above code.

  • Tomas, I edited again the question, I was passing only as parameter hash in the method Carregarlistvences, and I was not passing as parameter my filter too, so summarizing: I passed as parameter to minah var hash, Also Parametroswinning filter expired, and with that I did not lose the values of the parameters when I entered the method. I appreciate your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.