简体   繁体   English

比较C#中的数据表和字符串数组

[英]Compare a Datatable and string array in c#

I simply want to compare the first col of the datatable called "name" to items in the array. 我只是想将数据表中名为“名称”的第一个col与数组中的项目进行比较。 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. 可能要使用linq查询。

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. 因此,从上面的行r4和r5将从数据表中删除。

You can do couple of things with your code. 您可以使用代码来做几件事。

  • First use List<string> instead of ArrayList (see this for details) 首先使用List<string>代替ArrayList (见的详细信息)
  • 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 ,该表将具有基于您的电子邮件列表的行。

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

尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM