1

In c#,i want to delete a datatable from dataset,if all the values of the datatable are zero.

How to achieve above functionality

I am using this code to add values into datatable

for (int row = startRowParcel + 1; row <= endRowParcel; row++) {
    List<string> rateRow = new List<string>();

    for (int col = startColumnNsa; col <= endColumnNsa; col++) {
        if (Convert.ToString(ws.Cells[row, col].Value) == null)
            rateRow.Add("0");
        else if (Convert.ToString(ws.Cells[row, col].Value) == "1/2")
            rateRow.Add("0.5");
        else
            rateRow.Add(Convert.ToString(ws.Cells[row, col].Value));
    }

    tbPriority.Rows.Add(rateRow.ToArray());
}   

thanks in advance.

sschrass
  • 7,014
  • 6
  • 43
  • 62
sindhu jampani
  • 504
  • 3
  • 10
  • 30
  • Instead of deleting if all are zero why not check your list for values other than 0 and if true then add to your Datatable else dont? – Srb1313711 Feb 10 '14 at 12:28
  • The values are strings which are not convertible to `int`, so what means `zero` in this case? – Tim Schmelter Feb 10 '14 at 12:32
  • @Sindhujampani Look at the answer by TYY similar to my answer looks like the one that will be helpful for you – Srb1313711 Feb 10 '14 at 12:41
  • if((rateRow.AsEnumerable().Any(x => x != "0"))) { tbPmServices.Rows.Add(rateRow.ToArray()); }Actually i tried this.but it is adding the row even the entire row has "0" – sindhu jampani Feb 11 '14 at 07:12

4 Answers4

1

you can achieve this by using below code:

     for(int i=0;i<dt.rows.count;i++)
    {
          for(intj=0;j<=dt.columns.count;j++)
            {

             if( dt.rows[i][j]!=0)
            {
                 flag=1;
                 break;
              }

             }
    }

    if(flag==1)
    {
       // dont remove the table
    }

    else
    {

     ds.tables.remove(dt);
    }

}

Iterate through that datatable ,check for non zero values, if all are zero remove it else not

Hope this helps..

Sai Avinash
  • 4,683
  • 17
  • 58
  • 96
1

This LINQ approach finds all tables where all rows' fields are "0":

var allZeroTables = dsPriorities.Tables.Cast<DataTable>()
    .Where(tbl => tbl.AsEnumerable()
        .All(r => tbl.Columns.Cast<DataColumn>()
            .All(c => r.Field<string>(c) == "0")));
foreach (DataTable zeroTable in allZeroTables.ToList())
    dsPriorities.Tables.Remove(zeroTable);

Enumerable.All is a short circuiting method that stops on the first non-match.

Note that the ToList() is required since you cannot modify the DataSet's DataTableCollection from within the foreach without creating a new collection.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0
if (!(dt.AsEnumerable().Any(x => x.Field<double>("nameOfColumn") != 0)) {
    //delete data table
}

For more information see LINQ query on a DataTable

Community
  • 1
  • 1
mnieto
  • 3,744
  • 4
  • 21
  • 37
  • Instead of `!..Any(.. != 0)`, you could simply use `All(.. == 0)` – sloth Feb 10 '14 at 12:34
  • I used to use that before but found out it's much slower than a for...loop with an exit on first invalid result. But on small DataTable this performance hit shouldn't be too much visible. – Franck Feb 10 '14 at 12:36
  • `All()` will iterate, always, all the collection. `Any()` iterates only until the first occurrence. Yes, it's more obscure syntax, but in a large collection, `Any` will do better performance – mnieto Feb 10 '14 at 12:41
0
for (int row = startRowParcel + 1; row <= endRowParcel; row++) {
    List<string> rateRow = new List<string>();

    for (int col = startColumnNsa; col <= endColumnNsa; col++) {
        if (Convert.ToString(ws.Cells[row, col].Value) == null)
            rateRow.Add("0");
        else if (Convert.ToString(ws.Cells[row, col].Value) == "1/2")
            rateRow.Add("0.5");
        else
            rateRow.Add(Convert.ToString(ws.Cells[row, col].Value));
    }

    if (rateRow.Any(x=> x != "0"))
    tbPriority.Rows.Add(rateRow.ToArray());
} 
//Then you can check if tbPriority has any rows if it doesn't then remove it from the dataset
if (!tbPriority.Rows.Any())
    // delete dataTable

if rateRow contains any value other than "0" the rows will get added.

TYY
  • 2,702
  • 1
  • 13
  • 14