How to use Lambda expression in List<List<Object>?

Asked

Viewed 626 times

0

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

Method filling in the 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);

Someone help me pick up one item at a time without a Foreach ?

  • What result do you expect to get and what is it getting? Is there an error? Which one? Where do you want to do this that you ask for?

  • 2

    You want something like lstListItNota.Where(a => a.Any(b => b.Codigo == "A")).ToList();?

  • @bigown, actually was unable to implement the search even.

  • @Pablovargas, that’s what I’m talking about. I was able to do it like this: var lstListItNota = plstListItNota.Where(x => x.Exists(y => y.Cdclien == client.Cdclien)); - But you still have to create two Foreach loops to get the result I hope you will get.

  • 1

    I still don’t quite understand what you want, but it seems to me that it would only be a good idea to restructure this code a lot, and look there.

  • @bigown, I believe you have a better way of doing it according to your knowledge, but I see no other way of doing it. What would be the best way to structure the code ?

  • @Pablovargas, the code you wrote worked, that’s exactly what I needed. Thank you

Show 2 more comments

3 answers

2


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

lstListItNota.Where(a => a.Any(b => b.Codigo == "A")).ToList();
  • Using Exists() instead of the Any() method also worked. But, I left it with Any() itself.

  • I answered a question about the use of Contains these days, I did not use Exists in the examples http://answall.com/a/186100/5846

  • I took a look at this post and using Entityframework helps a lot. However, I’ll have to learn how to use it, because I don’t have that knowledge yet.

1

Hello, try the following:

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

select Many returns a collection of collections, i.e., you have a list of multiple lists that store the same type of objects, the Selectmany will return you a collection of these objects, no more lists of them.

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

I hope I’ve helped.

  • the way Pablo talked I was able to solve my problem. I made a new test with the code you passed to know another way to do, however, is not returned value, the collection is coming empty.

  • @Diegofarias you can post your code is a small mass of data to analyze better?

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 Itnote(), using the Entity Framework.

And to perform the foreach would be so

foreach (ItNota item in lista){

}
  • Hebert, I believe that in this case this code would not suit my scenario, because I have a script with n clients, and for each client has n notes. I created a List<List<Itnote>> to group all notes from each client, and then extract each List<Itnote> from each client.

  • How is this structure in the bank? Another table?

  • The way Pablo showed above has already solved the problem Herbert, thank you.

Browser other questions tagged

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