Comparing a Datatable and an ArrayList and removing values in C#
I have a Datatable that has multiple columns and rows but my focus is only on the column of email addresses. I also have an ArrayList of email addresses. I need to loop through and compare the email addresses in the ArrayList with the Datatable and either remove them from the Datatable if they both contain the same email address or create a new Datatable or ArrayList without those email addresses. How can this be done in C开发者_开发知识库#?
I think that ArrayList implements IEnumerable. Datatable has an asEnumerable (extension?) method. From there you can write a linq query. Perhaps a "select from datatable left join arraylist where arraylist is not null" type query.
OK, this is probably TOTAL overkill, but since I brought it up, I'll show you how to do it.
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable("TestTable");
dt.Columns.Add(new DataColumn("id",System.Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("email",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["id"] = 1;
r1["email"] = "one@mail.com";
dt.Rows.Add(r1);
DataRow r2 = dt.NewRow();
r2["id"] = 1;
r2["email"] = "two@mail.com";
dt.Rows.Add(r2);
DataRow r3 = dt.NewRow();
r3["id"] = 1;
r3["email"] = "three@mail.com";
dt.Rows.Add(r3);
DataRow r4 = dt.NewRow();
r4["id"] = 1;
r4["email"] = "four@mail.com";
dt.Rows.Add(r4);
DataRow r5 = dt.NewRow();
r5["id"] = 1;
r5["email"] = "five@mail.com";
dt.Rows.Add(r5);
var query = from row in dt.AsEnumerable()
join String a in al on row.Field<String>("email") equals a into ljtable
from a in ljtable.DefaultIfEmpty()
where a != null
select new { Email = row.Field<String>("email"), Checker = a == null ? "null" : a };
foreach (var v in query)
{
Console.WriteLine(String.Format("Email:{0} Left Join:{1}", v.Email, v.Checker));
}
Console.ReadLine();
}
}
精彩评论