Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I have a data table with n no of rows in it. I iterated the data table and got the index of the rows on certain condition and saved them in a list of integer.
Now i want to remove those columns from the data table where index lies in the list using a Linq Select statement.


Lets say datatable is DT
and List<int> is LstRemoveIndex

var FinalTable = DT.AsEnumerable()
                  .Where(?????)
                  .CopyToDataTable();


how to use LstRemoveIndex in the Where Condition or the select condition ???
Posted
Updated 15-Sep-15 23:08pm
v2

1 solution

C#
List<int> intlst = new List<int>();
intlst.Add(3);
intlst.Add(4);
intlst.Add(5);

DataTable dt = new DataTable();
dt.Columns.Add("num", typeof(Int32));
dt.Rows.Add(3);
dt.Rows.Add(4);
dt.Rows.Add(5);
dt.Rows.Add(6);

var FinalTable = (from x in dt.AsEnumerable()
                  where !intlst.Contains(x.Field<int32>("num"))
                  select x).CopyToDataTable();//output : 6 

Regards..
 
Share this answer
 
v2
Comments
Riya-Pandey 16-Sep-15 5:18am    
I don't have the Value for the the column of the row , just the index of the row.
so i can not use X.Field !!! :'(
Thanks7872 16-Sep-15 5:22am    
Did you mean 3,4,5 in list are indexes and you are suppose to remove data from data table on those indexes?
Riya-Pandey 16-Sep-15 6:51am    
Yes. There is one more problem that if lets say there are 10 rows in datatable
and i have 2,3,4,6,9 in the list.
Now if i start removing data according to index when the counter reaches 9
there will be only 6 rows in the table because 2,3,4,6 have been removed , also wrong entries are getting removed because as soon as 2 is deleted index 3 becomes index 2 and index 4 becomes index 3 and 4 is getting removed which is index 3 now.
Thanks7872 16-Sep-15 7:11am    
Index based removal is not a good idea. You have to find some other way to handle the same.
Riya-Pandey 17-Sep-15 1:42am    
Yup. It seems so. i found another way. Thanks for the advice :)

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