Skip whole line if specific cell value contains zero in datatable c #

I can skip the first line in datatable so as not to process further by iterating through datatable using the code below

 DataTable dt;
 foreach (DataRow r in dt.Rows.Cast<DataRow>().Skip(1))
 {
           //do something...
 }

but I need to skip the whole row to avoid further processing if the specific cell value is empty in that row in datatable

I am not sure how I can do this, can anyone help with this. thanks in advance

+4
source share
2 answers
        foreach (DataRow r in dt.Rows.Cast<DataRow>().Skip(1))
        {
            if (r["ThisColumnHas0Value"].ToString() == "0")
            {
                Console.WriteLine("SKIP");
                continue;
            }
            Console.Write("PROCESS");
        }
+1
source

Use Whereto filter out unwanted strings, e.g.

foreach (DataRow r in dt.Rows.Cast<DataRow>().Skip(1)
                             .Where(o => !string.IsNullOrEmpty(o["Column"].ToString())))
{
    ...
}

Even better, if you make a filter when querying data from the database, so these rows will never be loaded.

+1

Source: https://habr.com/ru/post/1690821/


All Articles