Check if the item exists in a List<>

Asked

Viewed 4,347 times

0

I have a list typed in C# and I need to search for an item through several filters. I demonstrate in codes:

List<LoteRecla> listLoteRecla = new List<LoteRecla>();

I tried using the method . Exists() filtering through only one field of this list, as below and worked normally.

    if (!listLoteRecla.Exists(x => x.est_lote.Equals(dtDados.Rows[i]["est_lote"].ToString())))

But I was asked to use more filters, I tried to do it the way below:

if (!listLoteRecla.Exists(
                                x => x.est_lote.Equals(dtDados.Rows[i]["est_lote"].ToString()) &&
                                x.id_log_unidade.Equals(dtDados.Rows[i]["id_log_unidade"]) &&
                                x.id_log_unid_local.Equals(dtDados.Rows[i]["id_log_unid_local"]) &&
                                x.loc_rua.Equals(dtDados.Rows[i]["loc_rua"]) &&
                                x.loc_num.Equals(dtDados.Rows[i]["loc_num"].ToString()) &&
                                x.loc_altura.Equals(dtDados.Rows[i]["loc_altura"].ToString()) &&
                                x.id_log_produto.Equals(dtDados.Rows[i]["id_log_produto"]) &&
                                x.id_log_produto_emb.Equals(dtDados.Rows[i]["id_log_produto_emb"])))
                            {

However this way is not working, I pass for equal data and should not enter the IF, but is entering.

Am I doing something wrong? Is there any other way to do this that I need?

Thank you.

  • 1

    Put one by one and see which causes a change. Maybe you are finding an unexpected value. Maybe you are using a wrong condition. You can’t know without access to your data. That is, you have to debug the code to find out what is happening. Evaluate the expressions in Debugger when you get to this point, make sure everything is according to what you expect. It may even be that the code has something wrong but we have no way of knowing.

  • ok, I’ll go adding one by one to test. Thank you.

3 answers

2


The correct form is using Any:

if (!listLoteRecla.Any(
                            x => x.est_lote.Equals(dtDados.Rows[i]["est_lote"].ToString()) &&
                            x.id_log_unidade.Equals(dtDados.Rows[i]["id_log_unidade"]) &&
                            x.id_log_unid_local.Equals(dtDados.Rows[i]["id_log_unid_local"]) &&
                            x.loc_rua.Equals(dtDados.Rows[i]["loc_rua"]) &&
                            x.loc_num.Equals(dtDados.Rows[i]["loc_num"].ToString()) &&
                            x.loc_altura.Equals(dtDados.Rows[i]["loc_altura"].ToString()) &&
                            x.id_log_produto.Equals(dtDados.Rows[i]["id_log_produto"]) &&
                            x.id_log_produto_emb.Equals(dtDados.Rows[i]["id_log_produto_emb"])))
                        {
  • I don’t see why it would work as Any and not with Exists, after all the behavior of both algorithms is similar. But this is not so much the fact that Any is preferable.

  • Any is an extensive method, it works for whatever Ienumerable, whereas Exists is a method of the List<> object that exists since the . NET 2.0. https://msdn.microsoft.com/en-us/library/vstudio/6sh2ey19(v=vs.100). aspx

  • @Tobymosque So you prefer to use an older method than a dedicated method, which works with greater synergy with the data provider?

  • I even said that Any is preferable and gave an up vote on his reply, just said that the return of the two should be the same, although Any present a superior performance.

  • @Tobymosque exactly, and thank you. Exists in this case should be discouraged.

Show 1 more comment

0

I did it that way and it worked.

if (!listLoteRecla.Exists(x => x.est_lote.Equals(dtDados.Rows[i]["est_lote"].ToString())) && 
    !listLoteRecla.Exists(x => x.id_log_unidade.Equals(dtDados.Rows[i]["id_log_unidade"])) &&
    !listLoteRecla.Exists(x => x.id_log_unid_local.Equals(dtDados.Rows[i]["id_log_unid_local"])) &&
    !listLoteRecla.Exists(x => x.loc_rua.Equals(dtDados.Rows[i]["loc_rua"])) &&
    !listLoteRecla.Exists(x => x.loc_num.Equals(dtDados.Rows[i]["loc_num"].ToString())) &&
    !listLoteRecla.Exists(x => x.loc_altura.Equals(dtDados.Rows[i]["loc_altura"].ToString())) &&
    !listLoteRecla.Exists(x => x.id_log_produto.Equals(dtDados.Rows[i]["id_log_produto"])) &&
    !listLoteRecla.Exists(x => x.id_log_produto_emb.Equals(dtDados.Rows[i]["id_log_produto_emb"]))){

But now I’m in another situation where I’m gonna need some help too.

I want to recover the index from the list line by filtering through the same fields above, tried with the method. Findindex() and it only works with a parameter, as below.

int index = listLoteRecla.FindIndex(x => x.est_lote.Equals(dtDados.Rows[i]["est_lote"].ToString()));

If I try to add more parameters to it it doesn’t work.

Is there any other method that does something like this? Or some example that I can use . Findindex() even?

  • If you have encountered another problem, edit your question with the new problem, do not answer your question with another question.

0

made using the idea of Predicate<> and it worked and even in Code Metrics the method became cleaner. It was like this:

Calling for:

int index = listLoteRecla.FindIndex(LogicaDeBuscaEditar);

Method:

private static bool LogicaDeBuscaEditar(LoteRecla obj)
        {
            if(obj.est_lote.Equals(dtRowGlobal["est_lote"].ToString()) &&
                obj.id_log_produto.Equals(Convert.ToInt64(dtRowGlobal["id_log_produto"])) &&
                obj.id_log_produto_emb.Equals(Convert.ToInt64(dtRowGlobal["id_log_produto_emb"])) &&
                obj.id_log_unid_local.Equals(Convert.ToInt64(dtRowGlobal["id_log_unid_local"])) &&
                obj.id_log_unidade.Equals(Convert.ToInt64(dtRowGlobal["id_log_unidade"])) &&
                obj.loc_altura.Equals(dtRowGlobal["loc_altura"].ToString()) &&
                obj.loc_num.Equals(dtRowGlobal["loc_num"].ToString()) &&
                obj.loc_rua.Equals(dtRowGlobal["loc_rua"].ToString()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

I thought it was organized this way because the whole validation rule was in another method.

Thank you.

Browser other questions tagged

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