LING SetField how to get it to work for more than one row

  • Thread starter Thread starter GG
  • Start date Start date
G

GG

This works
(from s1 in seq1
where s1.Field<string>("EmployeeName")=="Ahmed"
select s1).Single().SetField<string>("EmployeeName","Hema");

but this does not work
(from s1 in seq1
where s1.Field<string>("EmployeeName")=="Ahmed"
select s1).SetField<string>("EmployeeName","Hema");

Any ideas how to get the SetField to update all the rows?

Thanks
 
GG said:
This works
(from s1 in seq1
where s1.Field<string>("EmployeeName")=="Ahmed"
select s1).Single().SetField<string>("EmployeeName","Hema");

but this does not work
(from s1 in seq1
where s1.Field<string>("EmployeeName")=="Ahmed"
select s1).SetField<string>("EmployeeName","Hema");

Any ideas how to get the SetField to update all the rows?
You'll have to do a manual "for each"; LINQ is of no help here because you
want to modify the rows rather than return a new sequence. If you do want to
return a new sequence, you can't use DataRows for that since you can't clone
those, but you can use anonymously typed objects.

foreach (var row in dataTable.Rows) {
if (row.Field<string>("EmployeeName") == "Ahmed") {
row.SetField<string>("EmployeeName", "Hema");
}
}

If you really want you can use "select" or .Where() here, but I don't think
it makes things clearer.

Or to get a new sequence based on the rows:

from s1 in dataTable.AsEnumerable()
where s1.Field<string>("EmployeeName") == "Ahmed"
select new {
// other fields
EmployeeName = "Hema"
};
 
Back
Top