Delete row from a list in C #

Posted on

Question :

I have a list as described below
I would like to delete a line from the mylistnova1 list as condition is true.
I try to do as described below but generates error! Could you please help me?
Abs.
       public DataTable Query (DataTable table)
        {

        NewDb openDb3 = new NewDb();
        DataTable Carga01 = tabela.Copy();
        DataTable Carga02 = tabela.Copy();
        //Carrega  DataTablepara uma lista
        var mylist = tabela.AsEnumerable().ToList();
         var mylistnova = tabela.AsEnumerable().ToList();
          var mylistnova1 = tabela.AsEnumerable().ToList();

    //    var mylist1 = ResultadoNovo.AsEnumerable().ToList();
        var mylist3 = Carga02.AsEnumerable().ToList();

           using (MySqlConnection db = openDb3.AbrirConexaoMySql())
                {
                    try
                    {
                        db.Open();



        foreach (var Item in mylist3)
        {

            string renavamrecebe = Convert.ToString(Item.ItemArray[1]);
        }


        foreach (var item02 in mylist)
         {

         string renavanRecebido = item02.ItemArray[01].ToString();
         string ComparaceuNoCartorio = item02.ItemArray[20].ToString();
         Datarecebe = item02.ItemArray[20].ToString();
                 foreach (var item03 in mylistnova1)
                 {
                      string tipocomptaVendaRecebe = Convert.ToString(item03.ItemArray[20]);
                Datarecebe1 = Convert.ToString(item03.ItemArray[15]);

                        renavamContagem = Convert.ToInt32((MamData.MySql.DUT.SelectQuantidadeRenavan(db, renavanRecebido, Datarecebe1)));




                        if (renavamContagem > 1)
                        {

                            if (renavanRecebido == Convert.ToString(item03.ItemArray[1]) && tipocomptaVendaRecebe == "C")
                            {

                                // Marco as linhas a serem deletadas
                                mylistnova1.RemoveAll(x => x == item03.ItemArray[1]); 

                            }


                        }




                 }
         }
        }



                    catch
                    {

                    }
                    db.Close();
                }
           DataTable ResultadoNovo = mylistnova1.CopyToDataTable<DataRow>();
           return (ResultadoNovo);
    }


    public DataTable ConvertToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

}

}

    

Answer :

So I understand you want to do a validation on the list item mylistnova1 and if it is validated then you exclude it from the list.

In this case you can not delete from a list that is in the loop, for example:

(ERROR ISSUE)

foreach (var item in lista)
{
    lista.Remove(item);
}

  

{“Collection has been modified; the enumeration operation may not be
  executed. “}

In this case you can use .ToList() to make a memory copy of the list and run the copy, releasing the original list to be able to delete.

Here it erases the entire lista :

foreach (var item in lista.ToList())
{
     lista.Remove(item);
}

In your case I would do so:

foreach (var item03 in mylistnova1.ToList())
                 {
                      string tipocomptaVendaRecebe = Convert.ToString(item03.ItemArray[20]);
                Datarecebe1 = Convert.ToString(item03.ItemArray[15]);

                        renavamContagem = Convert.ToInt32((MamData.MySql.DUT.SelectQuantidadeRenavan(db, renavanRecebido, Datarecebe1)));




                        if (renavamContagem > 1)
                        {

                            if (renavanRecebido == Convert.ToString(item03.ItemArray[1]) && tipocomptaVendaRecebe == "C")
                            {

                                // Marco as linhas a serem deletadas
                                mylistnova1.Remove(item03); 

                            }


                        }




                 }

UPDATING

Since you have spoken of a slow query, then I believe you are dealing with a lot of data, check that .ToList() will maintain the desired performance.

    

Try this:

mylistnova1.Remove(mylistnova1.Single(item04 => item04.propriedade == comparador)); 

    

Leave a Reply

Your email address will not be published. Required fields are marked *