none
DataTable each cell validation using LINQ RRS feed

  • Question

  • I have a DataTable and I need to validate each data cell for its datatype. The existing code is as written below.

    foreach (DataRow row in dt.Rows)
                    {
                        bool validateDataRow = true;
                        DataRow newRow = dtTemplate.NewRow();
                        newRow["RowNumber"] = rowNo;

                        for (int colNo = 0; colNo < colCount; colNo++)
                        {
                            if (!row.IsNull(colNo) && row.ItemArray[colNo].ToString() != "")
                            {
                                try
                                {
                                    newRow[colNo] = Convert.ChangeType(row[colNo], dtTemplate.Columns[colNo].DataType);
                                }
                                catch
                                {
                                   
                                    validateDataRow = false;
                                }
                            }
                            else if (AllowNulls)
                            {
                                if (!dtTemplate.Columns[colNo].AllowDBNull)
                                {
                                    if (dtTemplate.Columns[colNo].DataType == typeof(string))
                                        newRow[colNo] = "";
                                    else
                                    {
                                       
                                        validateDataRow = false;
                                    }
                                }
                            }
                        }

    But this approach is taking very long time due to which I'm getting page wait message. I want to rewrite this logic using LINQ(or any better approach). Can someone help me with this? 

    Thanks in advance,

    Ravi

    Monday, August 29, 2016 11:56 AM

All replies

  • Hi ravi534,

    According to your description, you want to valid every field cell of datatable. As far as I know, LINQ could not increase the performance. If you want to use Linq, please refer the following code:

      var result = from p in dt.AsEnumerable()
                             select p;
    
                foreach (var row in result)
                {
                    bool validateDataRow = true;
                    DataRow newRow = dtTemplate.NewRow();
                    newRow["RowNumber"] = rowNo;
    
                    for (int colNo = 0; colNo < colCount; colNo++)
                    {
                        if (!row.IsNull(colNo) && row.ItemArray[colNo].ToString() != "")
                        {
                            try
                            {
                                newRow[colNo] = Convert.ChangeType(row[colNo], dtTemplate.Columns[colNo].DataType);
                            }
                            catch
                            {
                                validateDataRow = false;
                            }
                        }
                        else if (AllowNulls)
                        {
                            if (!dtTemplate.Columns[colNo].AllowDBNull)
                            {
                                if (dtTemplate.Columns[colNo].DataType == typeof(string))
                                    newRow[colNo] = "";
                                else
                                {
    
                                    validateDataRow = false;
                                }
                            }
                        }
                    }
                }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 30, 2016 3:10 AM
    Moderator