0

I simply want to compare the first col of the datatable called "name" to items in the array. They can be multiple rows in the datatable with the same name. After it has compared all items in the array it should delete the rows that were NOT from the datatable. The output result can be the datatable itself or list array (prefer this) however this should retain all the columns from datatable. Possibly want to use linq query.

code:

DataTable dt = new DataTable("TestTable");
dt.Columns.Add(new DataColumn("email",System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("type",System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("card",System.Type.GetType("System.String")));

ArrayList al = new ArrayList();
al.Add("[email protected]");
al.Add("[email protected]");
al.Add("[email protected]");
al.Add("[email protected]");

DataRow r1 = dt.NewRow(); r1["email"] = "[email protected]"; //addtype+card// dt.Rows.Add(r1);

DataRow r2 = dt.NewRow(); r2["email"] = "[email protected]"; //addtype+card// dt.Rows.Add(r2); 

DataRow r3 = dt.NewRow(); r3["email"] = "[email protected]"; //addtype+card// dt.Rows.Add(r3);

DataRow r4 = dt.NewRow(); r4["email"] = "[email protected]"; //addtype+card// dt.Rows.Add(r4);

DataRow r5 = dt.NewRow(); r5["email"] = "[email protected]"; //addtype+card// dt.Rows.Add(r5);

So from the above row r4 and r5 will be deleted from the datatable.

2
  • 1
    Any reason to use ArrayList and not List<string> in 2014 ? Commented Feb 3, 2014 at 14:12
  • Not any particular reason but just old coding Commented Feb 3, 2014 at 14:16

2 Answers 2

1

You can do couple of things with your code.

  • First use List<string> instead of ArrayList (see this for details)
  • Second add rows to your DataTable, currently you are creating new rows but you are not adding them.

Later with this query you can create a new DataTable which would have rows based on your List of emails.

DataTable dtNew = dt.AsEnumerable()
                   .Where(r => al.Contains(r.Field<string>("email")))
                   .CopyToDataTable();

So your complete code could be:

DataTable dt = new DataTable("TestTable");
dt.Columns.Add(new DataColumn("email", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("type", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("card", System.Type.GetType("System.String")));

List<string> al = new List<string>(); //Use List<string>
al.Add("[email protected]");
al.Add("[email protected]");
al.Add("[email protected]");
al.Add("[email protected]");

DataRow r1 = dt.NewRow(); r1["email"] = "[email protected]"; //addtype+card// dt.Rows.Add(r1);
dt.Rows.Add(r1);
DataRow r2 = dt.NewRow(); r2["email"] = "[email protected]"; //addtype+card// dt.Rows.Add(r2); //Add Row to DataTable
dt.Rows.Add(r2);
DataRow r3 = dt.NewRow(); r3["email"] = "[email protected]"; //addtype+card// dt.Rows.Add(r3);
dt.Rows.Add(r3);
DataRow r4 = dt.NewRow(); r4["email"] = "[email protected]"; //addtype+card// dt.Rows.Add(r4);
dt.Rows.Add(r4);
DataRow r5 = dt.NewRow(); r5["email"] = "[email protected]"; //addtype+card// dt.Rows.Add(r5);
dt.Rows.Add(r5);

DataTable dtNew = dt.AsEnumerable()
                   .Where(r => al.Contains(r.Field<string>("email")))
                   .CopyToDataTable();
Sign up to request clarification or add additional context in comments.

3 Comments

Habib thanks for the answer, I also have marked it as answer. BUT i want to ask you how would you do the opposite of the above now i.e. I have a list of IEnumerable<classB> and want to remove items from it that are not in dtNew ?? thanks
@user2906420, If I understand your new question properly , then you just have to reverse the condition like Where(r => !al.Contains(r.Field<string>("email"))). Notice the ! before al.Contains
Habib but this time I want the output to be in the IEnumerable list like this, i tried this but does not work: mlist = mlist.Where(r => !dtNew.Contains(r.Name));
0

Try

var resultTable = dt.AsEnumberable().Where(r => al.Contains(r.Field<string>("email")).Select(r => r).CopyToDataTable();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.