2

I have two datatable with conditions in application and want some processing for multiple rows to update column value.

For example:

I have datatable1 with 10000 rows. I want to filter rows by datatable.select("condition") and as per condition, I want to update row values.

If for any condition, I found 20 rows from datatable. I want to update those 20 records in one shot. Not in any loop. I have datarow array for those values to update in datable.

k-s
  • 2,192
  • 11
  • 39
  • 73
  • How exactly do you want to update the rows? This seems like something you could probably do with a dynamic query using the ``in`` keyword. – Kippie Sep 16 '13 at 14:11
  • 3
    How do you think you can do it without a loop? If framework provides some method also it will be internally doing a loop! – Sriram Sakthivel Sep 16 '13 at 14:11
  • 1
    maybe he means with lambda expressions.. but there will be anyway a loop – darkdog Sep 16 '13 at 14:16
  • @Sriram - Yes, I know but better way or anything or any method which I am not aware if! Thats the reason I posted question. – k-s Sep 16 '13 at 14:19
  • @darkdog - Thanks for your understanding. Something similar I am looking for. – k-s Sep 16 '13 at 14:19

3 Answers3

11

You can try out the following linq,

DataTable recTable = new DataTable();

// do stuff to populate table

recTable.Select(string.Format("[code] = '{0}'", someName)).ToList<DataRow>().ForEach(r => r["Color"] = colorValue);

You can substitute your columns and values here...

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
2

To Update Row With Multiple Condition use This

    datatable.Select(string.Format("[lineNo]='{0}' and [Position]>='{1}' ", lineNo, Position)).ToList<DataRow>().ForEach(r => r["Linetext"] ="Sample Text" );
sivabalan
  • 149
  • 2
  • 6
0

If you want to default a column value with abc use Expression, then you could use the below code.

dt.Columns.Add("ColumnName").Expression = "'abc'";

In case if you need to pass the value dynamically using a variable, you could use the below code.

string str = "abc";
dt.Columns.Add("ColumnName").Expression = "'" + str + "'";
Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86