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?
– Maniero
You want something like
lstListItNota.Where(a => a.Any(b => b.Codigo == "A")).ToList();
?– Pablo Tondolo de Vargas
@bigown, actually was unable to implement the search even.
– Diego Farias
@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.
– Diego Farias
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.
– Maniero
@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 ?
– Diego Farias
@Pablovargas, the code you wrote worked, that’s exactly what I needed. Thank you
– Diego Farias