Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to delete rows from Dataset table using Linq? pls help
Posted

If you have a DataTable why would you need to use Linq? Linq is not some unique all powerful language/tool that must be used.
 
Share this answer
 
You cannot directly delete the rows from datatable. However you can use this alternative trick with LINQ

C#
var query = from p in dataTable.AsEnumerable() 
                                    
where p.Field<string>("OrderNo").Trim() != orderN.Trim() 
                                   
 select p; 
                        
if (query.Count<datarow>() > 0) 
                        
{ 
                            
//Creating a table from the query 
                            
dataTable = query.CopyToDataTable<datarow>(); 
                        
} 
 
Share this answer
 
v3
Comments
[no name] 5-Jan-12 21:26pm    
Why can't you delete from a DataTable?

"...is as powerful as LINQ"?? You are using linq.

You haven't deleted anything with this solution.
bbirajdar 6-Jan-12 1:24am    
Mark, instead of downvoting my solution, you should have posted the solution with some code sample.
[no name] 6-Jan-12 8:34am    
Not all solutions require code. The down vote is justified because you didn't even answer the question posted. Live with it or post a better solution that answers the question.
bbirajdar 6-Jan-12 1:24am    
And if you can't 'read' the code above, try executing it. I also invite you to make performance tests on it with the other option using 'foreach' loop and deleting one DataRow at a time from the Datatable.
[no name] 6-Jan-12 8:35am    
If you can't "read" the question, then don't post a solution. No where was there any concern about performance. Your solution just didn't answer the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900