Search code examples
c#linqdatatablelinq-to-dataset

Get Specified row index of a column from a data table having values contained in a string


I have a data table having the two columns Select and Value.

Select Value
 0      213
 0      314
 0      282

I have an integer array called Ids={213,314} If the values of Ids occur in the data table column "values" then update the "select" column as 1. I have to do this using Linq. Please help


Solution

  • Linq is for querying, not for updating. So you have to do task in two steps. First is querying for rows which should be updated:

    var rows = from r in table.AsEnumerable()
               where Ids.Contains(r.Field<int>("Value"))
               select r;
    // Or lambda syntax
    // rows = table.AsEnumerable().Where(r => Ids.Contains(r.Field<int>("Value")))
    

    Second part is updating selected rows, which does not involve Linq:

    foreach(var row in rows)
        row.SetField("Select", 1);