Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update two columns in a DataTable using LINQ

Tags:

c#

datatable

I want to update two columns of DataTable in a single line using LINQ query. Currently I am using following two lines to do the same:

oldSP.Select(string.Format("[itemGuid] = '{0}'", itemGuid)).ToList<DataRow>().ForEach(r => r["startdate"] = stDate);
oldSP.Select(string.Format("[itemGuid] = '{0}'", itemGuid)).ToList<DataRow>().ForEach(r => r["enddate"] = enDate);

How can I do this in one line, using one Select?

like image 721
Jainendra Avatar asked Nov 08 '13 09:11

Jainendra


Video Answer


1 Answers

Use curly bracers to do two on more operations:

oldSP.Select(string.Format("[itemGuid] = '{0}'", itemGuid))
       .ToList<DataRow>()
       .ForEach(r => { r["enddate"] = enDate); r["startdate"] = stDate; });

But for code readability I would use old-fashioned foreach loop.

like image 101
gzaxx Avatar answered Sep 28 '22 19:09

gzaxx