Merge two lists c#

Asked

Viewed 2,067 times

1

I have 2 lists and would like to join them in a single list to be listed.

I created two lists because both present different conditions of the query and I could not relate them in a single list. Follow the code:

UnitOfWork unitAux = new UnitOfWork();

List<VW_PARCEIROSOFERTAS> bdofertasPrazo = new List<VW_PARCEIROSOFERTAS>();
List<VW_PARCEIROSOFERTAS> bdofertasValor= new List<VW_PARCEIROSOFERTAS>();

var dePrazo = System.Convert.ToDecimal(Prazo);
var deValor = System.Convert.ToDecimal(Valor);

if (!string.IsNullOrEmpty(Prazo.ToString()) && dePrazo > 0)

    bdofertasPrazo = unitAux.Vw_ParceirosOfertas.ConsultaOfertaPorProduto(_empresaAtiva, Produto)
                .Where(q => q.PRAZOINICIAL <= dePrazo && q.PRAZOFINAL >= dePrazo && q.TIPOFAIXAPRECO == "P").ToList();

//.Where(q => q.PRAZOINICIAL >= dePrazo && q.PRAZOFINAL <= dePrazo && q.TIPOFAIXAPRECO == "P").ToList();             

{
    if (!string.IsNullOrEmpty(Valor.ToString()) && deValor > 0)
        bdofertasValor = unitAux.Vw_ParceirosOfertas.ConsultaOfertaPorProduto(_empresaAtiva, Produto)
                    .Where(q => q.VALORINICIAL >= deValor && q.VALORFINAL <= deValor && q.TIPOFAIXAPRECO == "V").ToList();
}

2 answers

3

  • Note: To work, import the namespace using System.Linq;.

2


Use the Addrange:

List<VW_PARCEIROSOFERTAS> bdofertas = new List<VW_PARCEIROSOFERTAS>();
bdofertas.AddRange(bdofertasPrazo);
bdofertas.AddRange(bdofertasValor);

The collection whose elements should be added at the end of the List. A collection itself may not be null, but may contain elements that are null, if type T is a reference type.

  • 1

    A good idea is to instantiate the list by passing as a parameter the amount of elements it will have since this information is easily accessible. This way, you avoid creating arrays unnecessary internally.

Browser other questions tagged

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