get the unmatched records from DataTable1 by comparing DataTable2http://forums.asp.net/t/1580979.aspx/1?get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Thu, 22 Jul 2010 06:25:35 -040015809793983531http://forums.asp.net/p/1580979/3983531.aspx/1?get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2get the unmatched records from DataTable1 by comparing DataTable2 <p>Dear All,</p> <p>&nbsp; I want to get the unmatched records from DataTable1 by comparing DataTable2 </p> <p>eg :</p> <p>Table 1<br> </p> <p>CallId&nbsp;&nbsp; Destination&nbsp;&nbsp;&nbsp;&nbsp; Call type <br> </p> <p>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 15515643&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MOSE<br> </p> <p>2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 16464996&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MOSE</p> <p>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 46169789&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MOSTT<br> </p> <p>4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 48892269&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MOSTT</p> <p><br> </p> <p>Table 2</p> <p>CallId&nbsp;&nbsp; Destination&nbsp;&nbsp;&nbsp;&nbsp; Call type </p> <p>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 15515643&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Local</p> <p>4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 48892269&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; STD</p> <p><br> </p> <p>I want to match the above two table by using column &quot;CallId&quot; and I want the result as Below</p> <p>(Unmatched records of Table 1)<br> </p> <p>CallId&nbsp;&nbsp; Destination&nbsp;&nbsp;&nbsp;&nbsp; Call type </p> <p>2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 16464996&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MOSE</p> <p>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 46169789&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MOSTT</p> <p><br> </p> <p>How can I achive this by using LINQ ?????????<br> </p> <p><br> </p> <p>Please Help......<br> </p> <p><br> </p> 2010-07-21T05:26:05-04:003983609http://forums.asp.net/p/1580979/3983609.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>try this</p> <p><a href="http://www.dotnetspark.com/kb/705-compare-two-datatables-and-get-result.aspx">http://www.dotnetspark.com/kb/705-compare-two-datatables-and-get-result.aspx</a></p> <p><br> </p> <p><br> </p> 2010-07-21T06:19:31-04:003983682http://forums.asp.net/p/1580979/3983682.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>assuming you have a unique key for the datatables and dt1, dt2 are the datatables.</p> <p><pre class="prettyprint">List&lt;DataRow&gt; DifferentRows = new List&lt;DataRow&gt;(); List&lt;DataRow&gt; dt1Rows = dt1.Rows.OfType&lt;DataRow&gt;().ToList(); List&lt;DataRow&gt; dt2Rows = dt2.Rows.OfType&lt;DataRow&gt;().ToList(); DifferentRows.AddRange(dt1Rows.Where(a =&gt; (dt2Rows.FindAll(b =&gt; b[&quot;CallID&quot;] == a[&quot;CallID&quot;]).Count &lt;= 0))); DifferentRows.AddRange(dt2Rows.Where(a =&gt; (dt1Rows.FindAll(b =&gt; b[&quot;CallID&quot;] == a[&quot;CallID&quot;]).Count &lt;= 0)));</pre></p> <p>try that and see it works for you<br> </p> <p><br> </p> <p><br> </p> <p><br> </p> 2010-07-21T07:01:22-04:003983800http://forums.asp.net/p/1580979/3983800.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>Hai, </p> <p>&nbsp; Thanks for your response.</p> <p>&nbsp; I use the above code but the system hangs at the execution of the below line </p> <p>DifferentRows.AddRange(dt1Rows.Where(a =&gt; (dt2Rows.FindAll(b =&gt; b[&quot;CallID&quot;] == a[&quot;CallID&quot;]).Count &lt;= 0)));</p> <p><br> </p> <p>Plese guide me ...<br> </p> 2010-07-21T07:59:27-04:003983807http://forums.asp.net/p/1580979/3983807.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>how many rows do you have in those DataTables.<br> </p> <p><br> </p> 2010-07-21T08:03:06-04:003983843http://forums.asp.net/p/1580979/3983843.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>Hai,</p> <p>&nbsp;&nbsp; As of now i having 18000 rows. It may chance to more than this..</p> <p>&nbsp; I feel there may be a error in the Lambda expression.</p> <p><br> </p> <p>While i view the same line in the Quick watch i show the exception as &quot;Expression cannot contain lambda expressions&quot;</p> 2010-07-21T08:19:21-04:003983858http://forums.asp.net/p/1580979/3983858.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>I think it will take some time to compare as its a row by row comparison.</p> <p>I'm not sure if there is any optimised way to do.</p> <p>If you are using Oracle, you can easily do minus query</p> <p>SELECT A,B FROM DUAL</p> <p>MINUS</p> <p>SELECT C,D FROM DUAL<br> </p> <p>I'll check if there is any other better method to do this<br> </p> 2010-07-21T08:28:55-04:003983875http://forums.asp.net/p/1580979/3983875.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>Thanks for your reply.</p> <p>I don't want to do this in backend (SQL). I try to achive this in LINQ itself..</p> <p><br> </p> <p>Once again I thank you..<br> </p> 2010-07-21T08:40:13-04:003983888http://forums.asp.net/p/1580979/3983888.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>Hai,</p> <p>Is there any chance to achive the result by using JOIN (Join the two DataTable) in LINQ&nbsp; ??<br> </p> 2010-07-21T08:49:39-04:003983889http://forums.asp.net/p/1580979/3983889.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>Hai,</p> <p>Is there any chance to achive the result by using JOIN (Join the two DataTable) in LINQ&nbsp; ??</p> 2010-07-21T08:50:45-04:003983907http://forums.asp.net/p/1580979/3983907.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>Just try&nbsp;Left Outer Join approach&nbsp;in LINQ</p> <p>Not sure about performance ..For me its working fine with 18,00 rows table though ..</p> <pre class="prettyprint">// Converting your DataTable Table1 to IEnumerable&lt;&gt; var table1Values = from c in Table1.AsEnumerable() select new { callID = c.Field&lt;string&gt;(&quot;CallId&quot;), destination = c.Field&lt;string&gt;(&quot;Destination&quot;), callType = c.Field&lt;string&gt;(&quot;Call type&quot;) }; // Converting your DataTable Table2 to IEnumerable&lt;&gt; var table2Values = from c in Table2.AsEnumerable() select new { callID = c.Field&lt;string&gt;(&quot;CallId&quot;), destination = c.Field&lt;string&gt;(&quot;Destination&quot;), callType = c.Field&lt;string&gt;(&quot;Call type&quot;) }; // Doing a Left Outer Join on Table1 and Table2 // And then selecting the rows which have null in right hand side var table1OnlyValues = from c in table1Values join d in table2Values on c.callID equals d.callID into cds from f in cds.DefaultIfEmpty() where f == null select new { callID = c.callID, destination = c.destination, callType = c.callType, }; foreach (var table1onlyValue in table1OnlyValues) { Response.Write(table1onlyValue.callID &#43; &quot;,&quot;); Response.Write(table1onlyValue.destination &#43; &quot;,&quot;); Response.Write(table1onlyValue.callType); Response.Write(&quot;&lt;/br&gt;&quot;); }</pre> <p><br> &nbsp;</p> 2010-07-21T09:00:12-04:003983917http://forums.asp.net/p/1580979/3983917.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>you can try this way which should be faster</p> <p><pre class="prettyprint">IEnumerable&lt;DataRow&gt; dt1Enum = dt1.AsEnumerable(); IEnumerable&lt;DataRow&gt; dt2Enum = dt2.AsEnumerable(); DataTable dtUncommon = dt1Enum.Except(dt2Enum).CopyToDataTable();</pre></p> <p><a href="http://rsanidad.wordpress.com/2007/10/16/linq-except-and-intersect/">http://rsanidad.wordpress.com/2007/10/16/linq-except-and-intersect/</a></p> <p>it should be mix of both I think.<br> </p> <p><br> </p> 2010-07-21T09:06:14-04:003984019http://forums.asp.net/p/1580979/3984019.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>Hai,</p> <p>This code returns the first table (whole table rows) as result. In this where I should give the comparator field like &quot;CallId&quot;. </p> <p>where CallId is the column which i have to compare with the second table.<br> </p> 2010-07-21T09:46:52-04:003984071http://forums.asp.net/p/1580979/3984071.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p><br> <strong>Enumerable.Except&lt;TSource&gt;</strong> Method -</p> <p>Produces the set difference of two sequences by using the default equality comparer to compare values.</p> <p>So here Both the sets have DataRow Objects.<br> Say your first set has 4 DataRow objects and your second set have 2 dataRow objects&nbsp; <br> The Objects in your second set is different from objects in your first set even though they may have similar values.<br> That is the reason why Except operator returns all the values in the first set.</p> <p>If you want to compare sequences of objects of some custom data type, you can implement the IEqualityComparer&lt;(Of &lt;(T&gt;)&gt;) generic interface in your class.<br> if you want this approach to work you may have to create your own custom type, Implement IEqualityComparer interface and use Except operator on this class.</p> 2010-07-21T10:14:23-04:003984128http://forums.asp.net/p/1580979/3984128.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>Hai,</p> <p><br> </p> <p>This will work for me, thanks.</p> <p><br> </p> <p><span>How can i convert the &quot;var&nbsp;table1OnlyValues&quot;&nbsp; to DataTable object ???<br> </span></p> 2010-07-21T10:50:06-04:003984219http://forums.asp.net/p/1580979/3984219.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p></p> <blockquote><span class="icon-blockquote"></span> <h4>mohanaraj@3485</h4> <span>How can i convert the &quot;var&nbsp;table1OnlyValues&quot;&nbsp; to DataTable object ???</span></blockquote> &nbsp;<pre class="prettyprint">DataTable Dt = new DataTable(); Dt.Columns.Add(&quot;CallId&quot;); Dt.Columns.Add(&quot;Destination&quot;); Dt.Columns.Add(&quot;Call type&quot;); foreach (var table1onlyValue in table1OnlyValues) { Dt.Rows.Add(table1onlyValue.callID, table1onlyValue.destination, table1onlyValue.callType); }</pre> <p><br> &nbsp; </p> <p>&nbsp;</p> 2010-07-21T11:33:31-04:003985458http://forums.asp.net/p/1580979/3985458.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>Hai,</p> <p>Thanks.</p> <p>Is any other option to convert the Var to DataTable without looping???</p> <p>Or Any other option to use the LINQ as IEnuberable&lt;DataRow&gt; as <br> </p> <p>IEnumerable&lt;DataRow&gt; RoamingQuery = from d in ..............................<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select d</p> <p>for the above case (Your answer for get unmatched records) ??</p> <p><br> </p> 2010-07-22T03:04:41-04:003985583http://forums.asp.net/p/1580979/3985583.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p></p> <blockquote><span class="icon-blockquote"></span> <h4>mohanaraj@3485</h4> Is any other option to convert the Var to DataTable without looping??? <p>Or Any other option to use the LINQ as IEnuberable&lt;DataRow&gt; as <br> </p> <p>IEnumerable&lt;DataRow&gt; RoamingQuery = from d in ..............................<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select d</p> <p>for the above case (Your answer for get unmatched records) ??</p> </blockquote> &nbsp; <p></p> <p>Hi,</p> <p>Actually For converting LINQ to DataTable, There is no direct solution.</p> <p>There is a method <strong>CopyToDataTable&lt;T&gt;,</strong>&nbsp; But that will work only for <strong>IEnumerable&lt;DataRows&gt; </strong>and you cannot directly cast your LINQ query result which is an&nbsp;<strong>IEnumerable&lt;(Anonymous Type)&gt; </strong>(In our case)<pre class="prettyprint"><strong> </strong></pre>to&nbsp;&nbsp;<STRONG>IEnumerable&lt;DataRows&gt;&nbsp;</STRONG></P> <P>Even if we try a direct&nbsp;cast, It will return null.</P> <P>However there is a solution mentioned in <A class="" href="http://blogs.msdn.com/b/aconrad/archive/2007/09/07/science-project.aspx" target=_blank mce_href="http://blogs.msdn.com/b/aconrad/archive/2007/09/07/science-project.aspx">msdn</A></P> <P>Here the idea is to&nbsp;implement <STRONG>CopyToDataTable&lt;T&gt;</STRONG> when the generic type T is not a DataRow.</P> <P>You need to&nbsp;write two classes like this for extending the <STRONG>CopyToDataTable&lt;T&gt; </STRONG>method:</P><pre class="prettyprint">public static class DataSetLinqOperators { public static DataTable CopyToDataTable&lt;T&gt;(this IEnumerable&lt;T&gt; source) { return new ObjectShredder&lt;T&gt;().Shred(source, null, null); } public static DataTable CopyToDataTable&lt;T&gt;(this IEnumerable&lt;T&gt; source, DataTable table, LoadOption? options) { return new ObjectShredder&lt;T&gt;().Shred(source, table, options); } } </pre> <P><BR>&nbsp;</P><pre class="prettyprint">public class ObjectShredder&lt;T&gt; { private FieldInfo[] _fi; private PropertyInfo[] _pi; private Dictionary&lt;string, int&gt; _ordinalMap; private Type _type; public ObjectShredder() { _type = typeof(T); _fi = _type.GetFields(); _pi = _type.GetProperties(); _ordinalMap = new Dictionary&lt;string, int&gt;(); } public DataTable Shred(IEnumerable&lt;T&gt; source, DataTable table, LoadOption? options) { if (typeof(T).IsPrimitive) { return ShredPrimitive(source, table, options); } if (table == null) { table = new DataTable(typeof(T).Name); } // now see if need to extend datatable base on the type T + build ordinal map table = ExtendTable(table, typeof(T)); table.BeginLoadData(); using (IEnumerator&lt;T&gt; e = source.GetEnumerator()) { while (e.MoveNext()) { if (options != null) { table.LoadDataRow(ShredObject(table, e.Current), (LoadOption)options); } else { table.LoadDataRow(ShredObject(table, e.Current), true); } } } table.EndLoadData(); return table; } public DataTable ShredPrimitive(IEnumerable&lt;T&gt; source, DataTable table, LoadOption? options) { if (table == null) { table = new DataTable(typeof(T).Name); } if (!table.Columns.Contains("Value")) { table.Columns.Add("Value", typeof(T)); } table.BeginLoadData(); using (IEnumerator&lt;T&gt; e = source.GetEnumerator()) { Object[] values = new object[table.Columns.Count]; while (e.MoveNext()) { values[table.Columns["Value"].Ordinal] = e.Current; if (options != null) { table.LoadDataRow(values, (LoadOption)options); } else { table.LoadDataRow(values, true); } } } table.EndLoadData(); return table; } public DataTable ExtendTable(DataTable table, Type type) { // value is type derived from T, may need to extend table. foreach (FieldInfo f in type.GetFields()) { if (!_ordinalMap.ContainsKey(f.Name)) { DataColumn dc = table.Columns.Contains(f.Name) ? table.Columns[f.Name] : table.Columns.Add(f.Name, f.FieldType); _ordinalMap.Add(f.Name, dc.Ordinal); } } foreach (PropertyInfo p in type.GetProperties()) { if (!_ordinalMap.ContainsKey(p.Name)) { DataColumn dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name] : table.Columns.Add(p.Name, p.PropertyType); _ordinalMap.Add(p.Name, dc.Ordinal); } } return table; } public object[] ShredObject(DataTable table, T instance) { FieldInfo[] fi = _fi; PropertyInfo[] pi = _pi; if (instance.GetType() != typeof(T)) { ExtendTable(table, instance.GetType()); fi = instance.GetType().GetFields(); pi = instance.GetType().GetProperties(); } Object[] values = new object[table.Columns.Count]; foreach (FieldInfo f in fi) { values[_ordinalMap[f.Name]] = f.GetValue(instance); } foreach (PropertyInfo p in pi) { values[_ordinalMap[p.Name]] = p.GetValue(instance, null); } return values; } } </pre> <P><BR>Then you can directly&nbsp;call the method on our LINQ Query result</P> <P>&nbsp;</P><pre class="prettyprint"> // Create a table from the query. DataTable boundTable = table1OnlyValues.CopyToDataTable();</pre> </p> <p><br> &nbsp;</p> 2010-07-22T04:49:01-04:003985695http://forums.asp.net/p/1580979/3985695.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>Hai,</p> <p>Thanks for your reply.</p> <p>I need your help to Delete the rows present in the DataTable, after filter those record from the DataTable.</p> <p>eg: In DataTable1 have 100 records by using LINQ I filter the 10 records by condition as below </p> <p>IEnumerable&lt;DataRow&gt; MobileQuery = (from d in dtBillDetail.AsEnumerable()<br> where (String.Compare(d.Field&lt;string&gt;(&quot;Destination&quot;), &quot;MOBILE&quot;, StringComparison.InvariantCultureIgnoreCase) == 0) select d&nbsp;&nbsp; );</p> <p>I want to detlete the rows present in the &quot;MobileQuery&quot; from DataTable1 using LINQ or Witout Looping those.<br> </p> 2010-07-22T06:21:14-04:003985702http://forums.asp.net/p/1580979/3985702.aspx/1?Re+get+the+unmatched+records+from+DataTable1+by+comparing+DataTable2Re: get the unmatched records from DataTable1 by comparing DataTable2 <p>you can do this</p> <p><pre class="prettyprint">(from d in dtBillDetail.AsEnumerable() where (String.Compare(d.Field&lt;string&gt;(&quot;Destination&quot;), &quot;MOBILE&quot;, StringComparison.InvariantCultureIgnoreCase) == 0) select d).ToList().ForEach(a =&gt; dtBillDetail.Rows.Remove(a));</pre></p> &lt;div&gt;&lt;/div&gt; <pre class="prettyprint">(from d in dtBillDetail.AsEnumerable() where (String.Compare(d.Field&lt;string&gt;(&quot;Destination&quot;), &quot;MOBILE&quot;, StringComparison.InvariantCultureIgnoreCase) == 0) select d).ToList().ForEach(a =&gt; dtBillDetail.Rows.Remove(a));</pre> <p>If you get any error saying that Enumeration Value cannot be altered blah blah,</p> <p>you can do this</p><p><pre class="prettyprint">DataTable dtBillDetailCopy = dtBillDetail.Copy(); (from d in dtBillDetailCopy.AsEnumerable() where (String.Compare(d.Field&lt;string&gt;("Destination"), "MOBILE", StringComparison.InvariantCultureIgnoreCase) == 0) select d).ToList().ForEach(a =&gt; dtBillDetail.Rows.Remove(a));</pre>Try the first one and see if it works for you.</p> 2010-07-22T06:25:35-04:00