faizan ahmad

Usually the things which were not a straight Google

  Home  |   Contact  |   Syndication    |   Login
  17 Posts | 0 Stories | 68 Comments | 0 Trackbacks

News

Archives

Post Categories

.NET

ASP.NET

Here is a extension method which can be used to perform the  Delete operation on a DataTable just like the select i.e. using a filterExpression:

using

System;

using

System.Collections.Generic;

using

System.Data;

using

System.Data.SqlClient;

public static class MyExtensions

{

   /// <summary>

   /// Delete row based on filterExpression

   /// </summary>

   /// <param name="dt"></param>

   /// <param name="filterExpression"> just like select </param>

   /// <returns> number of deleted rows </returns>

   public static int Delete( this DataTable dt, string filterExpression)

   {

      int nor=0; // number of rows deleted

      DataRow [] toBeDeleted;

      toBeDeleted = dt.Select(filterExpression);

      if (toBeDeleted.Length > 0)

      {

         foreach ( DataRow dr in toBeDeleted)

            {

               dt.Rows.Remove(dr);

               nor++;

            }

      }

      return nor;

   }

 }

 

posted on Monday, December 12, 2011 5:34 PM