How to use Lambda expression in ListListobject?

0

Good afternoon I'm developing an application, which returns a list of note items list, however, I need to populate some fields with the values of each list of items at a time, however, I'm not able to implement the Select () or Where () of the list object?

Method that populates list:

public List<List<ItNota>> SelecionaDadosItNota(out String pstrMsg, out Boolean   pbooRetorno, Util.ObjTransf pobjTransf, List<Cliente> plstCliente)
{
    List<List<ItNota>> lstListItNota = default(List<List<ItNota>>);

    SqlConnection conn = ConexaoBD.CriarConexao(out pstrMsg, out pbooRetorno);

    if (pbooRetorno)
    {
        using (conn)
        {
            using (SqlCommand cmd = new SqlCommand("uspCtzSelectDadosProd", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@cd_emp", null);
                cmd.Parameters.AddWithValue("@nu_rom", null);
                cmd.Parameters.AddWithValue("@cd_clien", null);

                try
                {
                    lstListItNota = new List<List<ItNota>>();

                    foreach (var cliente in plstCliente)
                    {
                        cmd.Parameters["@cd_emp"].Value = pobjTransf.CdEmp;
                        cmd.Parameters["@nu_rom"].Value = pobjTransf.NuRom;
                        cmd.Parameters["@cd_clien"].Value = cliente.CdClien;

                        using (SqlDataReader rd = cmd.ExecuteReader())
                        {
                            if (rd.HasRows)
                            {
                                List<ItNota> lstItNota = new List<ItNota>();

                                while (rd.Read())
                                {
                                    ItNota itNota = new ItNota();

                                     itNota.CdClien = cliente.CdClien;
                                     itNota.Seq = rd["seq"].ToString();
                                     itNota.Codigo = rd["codigo"].ToString();
                                     itNota.Descricao = rd["descricao"].ToString();
                                     itNota.Valor = rd["valor"].ToString();
                                     itNota.Nf = rd["nf"].ToString();
                                     itNota.Perecivel = rd["perecivel"].ToString();
                                     itNota.Embarcador = rd["embarcador"].ToString();

                                     lstItNota.Add(itNota);
                                }
                                lstListItNota.Add(lstItNota);
                            }
                            pbooRetorno = true;
                        }                                
                    }
                }
                catch (SqlException ex)
                {
                    pstrMsg = ex.Message;
                    pbooRetorno = false;
                }
            }
        }
    }
    else
    {
        conn.Close();
    }
    return lstListItNota;
}

// Busca os dados dos itens da nota
List<List<ItNota>> lstListItNota = seleciona.SelecionaDadosItNota(out pstrMsg, out pbooRetorno, pobjTransf, lstCliente);

Does anyone help me get one item at a time without a Foreach?

    
asked by anonymous 23.02.2017 / 19:39

3 answers

2

Basically what you need is to make a Where in your lstListItNota and making the conditions in Any :

lstListItNota.Where(a => a.Any(b => b.Codigo == "A")).ToList();
    
24.02.2017 / 13:10
1

Hello, try the following:

lstListItNota.SelectMany(lista => lista).Where(item => item.Codigo == "A")).ToList();

Select many returns a collection of collections, that is, you have a list with multiple lists that store the same type of objects, SelectMany will return you a collection of these objects, no more than lists of them.

Then you will be able to use the other extension methods as you usually do.

I hope I have helped.

    
02.03.2017 / 13:50
0

Could do so

List<ItNota> lista = conn.TABELA.Select(x=> new ItNota() {CdClien  = x.CdClien}).ToList();

So you would return a list of type ItNota (), using the Entity Framework.

And to do foreach would be like this

foreach (ItNota item in lista){

}
    
23.02.2017 / 21:22