Question

I have situation, I need my DataRows (string columns) to be HTMLEncoded. Can we update entire cells of a DataRow in one go using LINQ or any other way?

Basically I want to avoid loops.

I have a datatable oDt. DataTable have following columns : id_season, season, modifiedon(date);

To save this dataTable i have a function

Save Table(DataTable oDT){
    //Here I have to update modifedon column to DateTime.now;
     foreach(datarow dr in oDT.Rows)
     {
               dr[modifiedon] = DateTime.now;
     }
    // I need to avoid this loop as datatable can have 35000 + records
}
Was it helpful?

Solution

You must update every element, so you are are looking at O(n) time regardless. On my system, updating 35,000 elements takes between 0.01 and 0.03 seconds, 350,000 items takes .1 to .3 seconds, etc. You can get a small performance boost by getting the value of DateTime.Now outside the loop like this:

var now = DateTime.Now;
foreach(DataRow dr in oDT.Rows)
{
    dr["ModifiedOn"] = now;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top