How to delete rows from DataTable with LINQ?

LINQ is not for deleting or modifying - it is for querying data. With LINQ you can select data which should be deleted, and then delete those data manually (e.g. in foreach loop or with ForEach list extension):

var query = dTable.AsEnumerable().Where(r => r.Field<string>("col1") == "ali");

foreach(var row in query.ToList())
   row.Delete();

UPDATE: Also with LINQ to DataSet you can select all rows which should stay in table and create new DataTable from those rows:

var table = dTable.AsEnumerable()
                  .Where(r => r.Field<string>("col1") != "ali")
                  .CopyToDataTable();

Try this inline lambda code with extension methodes:

  dTable.AsEnumerable().Where(r => r.Field("col1") == "ali").ToList().ForEach(row => row.Delete());
  dTable.AcceptChanges();

you use for loop or while loop to delete rows but not foreach

below is non linq solution

dTable= dTable.Select("col1 <> 'ali'").CopyToDataTable();

LINQ

dTable = dTable.AsEnumerable().Where(r => r.Field<string>("col1") != "ali").CopyToDataTable();

Tags:

C#

Linq

Asp.Net