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("one@mail.com");
al.Add("two@mail.com");
al.Add("nine@mail.com");
al.Add("ten@mail.com");

DataRow r1 = dt.NewRow(); r1["email"] = "one@mail.com"; //addtype+card// dt.Rows.Add(r1);

DataRow r2 = dt.NewRow(); r2["email"] = "one@mail.com"; //addtype+card// dt.Rows.Add(r2); 

DataRow r3 = dt.NewRow(); r3["email"] = "two@mail.com"; //addtype+card// dt.Rows.Add(r3);

DataRow r4 = dt.NewRow(); r4["email"] = "four@mail.com"; //addtype+card// dt.Rows.Add(r4);

DataRow r5 = dt.NewRow(); r5["email"] = "five@mail.com"; //addtype+card// dt.Rows.Add(r5);

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

user2906420
  • 1,249
  • 6
  • 27
  • 44

2 Answers2

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("one@mail.com");
al.Add("two@mail.com");
al.Add("nine@mail.com");
al.Add("ten@mail.com");

DataRow r1 = dt.NewRow(); r1["email"] = "one@mail.com"; //addtype+card// dt.Rows.Add(r1);
dt.Rows.Add(r1);
DataRow r2 = dt.NewRow(); r2["email"] = "one@mail.com"; //addtype+card// dt.Rows.Add(r2); //Add Row to DataTable
dt.Rows.Add(r2);
DataRow r3 = dt.NewRow(); r3["email"] = "two@mail.com"; //addtype+card// dt.Rows.Add(r3);
dt.Rows.Add(r3);
DataRow r4 = dt.NewRow(); r4["email"] = "four@mail.com"; //addtype+card// dt.Rows.Add(r4);
dt.Rows.Add(r4);
DataRow r5 = dt.NewRow(); r5["email"] = "five@mail.com"; //addtype+card// dt.Rows.Add(r5);
dt.Rows.Add(r5);

DataTable dtNew = dt.AsEnumerable()
                   .Where(r => al.Contains(r.Field<string>("email")))
                   .CopyToDataTable();
Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 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 and want to remove items from it that are not in dtNew ?? thanks – user2906420 Feb 03 '14 at 14:50
  • @user2906420, If I understand your new question properly , then you just have to reverse the condition like `Where(r => !al.Contains(r.Field("email")))`. Notice the `!` before `al.Contains` – Habib Feb 03 '14 at 14:53
  • 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)); – user2906420 Feb 03 '14 at 15:01
0

Try

var resultTable = dt.AsEnumberable().Where(r => al.Contains(r.Field<string>("email")).Select(r => r).CopyToDataTable();
Marko Juvančič
  • 5,792
  • 1
  • 25
  • 41