Thursday, July 24, 2014

query to datatable using LINQ C# ; DataTable Clone and copy



       


private DataTable DataForXXX(DataTable dt,string queryParam)
{
            //Get All Data?  just return
            if (queryParam.Equals(allCities)) return dt;

            // Clones the structure of the DataTable,
            // including all DataTable schemas and constraints.
            DataTable dtCopy = dt.Clone();


           // query to DataTable
            var results = from DataRow myRow in dt.Rows
                          where branch.Equals(
myRow["Branch"] as string,StringComparison.CurrentCultureIgnoreCase)
                          select myRow;


            foreach (var row in results)
            {
               // Use ImportRow otherwise error:
               // "This Row already belongs to another table"
                dtCopy.ImportRow(row);
            }

            return dtCopy;

}

No comments:

Post a Comment