比较C#中的数据表和字符串数组 [英] Compare a Datatable and string array in c#

查看:51
本文介绍了比较C#中的数据表和字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想将数据表中名为名称"的第一个col与数组中的项目进行比较.它们可以是数据表中具有相同名称的多行.在比较数组中的所有项目后,它应该删除不是从数据表中的行.输出结果可以是数据表本身或列表数组(首选此列表),但是这应保留数据表中的所有列.可能想使用linq查询.

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.

代码:

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);

因此,从上面的行r4和r5将从数据表中删除.

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

推荐答案

您可以使用代码来做几件事.

You can do couple of things with your code.

  • 首先使用 List< string> 而不是 ArrayList (请参阅
  • 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.

此查询之后,您可以创建一个新的 DataTable ,该表将基于您的电子邮件列表中的行.

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();

这篇关于比较C#中的数据表和字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆