删除数据表的重复列值 [英] Remove duplicate column values of a datatable

查看:68
本文介绍了删除数据表的重复列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下数据:

名称部门
John Alter D01
Prakash Kiran D01
Steffy Cold D01
Julia Roberts D02
Candy Mitchel D02
Monika Diesel D03


我想要如下:

名称部门
John Alter D01
Prakash Kiran
Steffy Cold
Julia Roberts D02
Candy Mitchel
Monika Diesel D03


当它由部门订购时,不应该为同一部门重复。
但该行应该在那里,只有在Department列下的下一行中有相同的值时,才需要BLANK


帮助ASAP





我的尝试:



使用以下代码来自某人..但它不起作用





 DataTable newDt = dt.Clone(); 
for int i = 0 ; i < dt.Rows.Count; i ++)
{
DataRow newRow = newDt.NewRow();
newRow [ 名称] = dt.Rows [i] [ 名称];
int originalIndex = dt.AsEnumerable()。ToList()。FindIndex(x = > x [ Department] == dt.Rows [i] [ 部门]);
if (originalIndex < i)
{
newRow [ 部门] = ;
}
else
{
newRow [ Department] = dt.Rows [i] [ ];
}
newDt.Rows.Add(newRow);
}

解决方案

试试



 DataTable dt = new DataTable(); 
dt.Columns.Add(名称);
dt.Columns.Add(部门);
dt.Rows.Add(John Alter,D01);
dt.Rows.Add(Prakash Kiran,D01);
dt.Rows.Add(Steffy Cold,D01);
dt.Rows.Add(Julia Roberts,D02);
dt.Rows.Add(Candy Mitchel,D02);
dt.Rows.Add(Monika Diesel,D03);
var departments = dt.AsEnumerable()。选择(k => k.Field< string>(Department))。Distinct()。OrderBy(k => k).ToArray();

foreach(部门中的字符串部门)
{
int i = 0;
foreach(dt.Rows中的DataRow行)
{
if(row [Department]。ToString()== dept){
if(i> 0) {
row [Department] =;
}
i ++;
}
}
}


该代码是我的,正如我们在评论讨论中所建立的那样,用 .Equals 方法替换 == 运算符。



为了将来参考,这里有为什么有效:

返回类型 x [部门] dt.Rows [i] [部门] 对象(而不是字符串,因为表格可以包含其他数据)。在对象上使用 == 时,这相当于调用 Object.ReferenceEquals [ ^ ] - 这里的实际类型并不重要是一个字符串,仍然使用Object.ReferenceEquals(这是我在编写原始代码时忘记的)。但是,如果您使用 .Equals ,那么它使用 String.Equals [ ^ ](这是你想要的),因为你有一个字符串,而String类型会覆盖Object。等于

I have a datatable like below:

Name               Department
John Alter         D01
Prakash Kiran      D01
Steffy Cold        D01
Julia Roberts      D02
Candy Mitchel      D02
Monika Diesel      D03


I want it like below: 

Name               Department
John Alter         D01
Prakash Kiran      
Steffy Cold        
Julia Roberts      D02
Candy Mitchel      
Monika Diesel      D03


when its ordered by a department, it should not repeat for the same department.
But the row should be there, only if there is same value in next row under Department column, that needs to be BLANK

help ASAP



What I have tried:

Used the below code from someone.. but its not working



DataTable newDt = dt.Clone();
for (int i = 0; i < dt.Rows.Count; i++)
{
    DataRow newRow = newDt.NewRow();
    newRow["Name"] = dt.Rows[i]["Name"];
    int originalIndex = dt.AsEnumerable().ToList().FindIndex(x => x["Department"] == dt.Rows[i]["Department"]);
    if (originalIndex < i)
    {
        newRow["Department"] = "";
    }
    else
    {
        newRow["Department"] = dt.Rows[i]["Department"];
    }
    newDt.Rows.Add(newRow);
}

解决方案

try

DataTable dt = new DataTable();
           dt.Columns.Add("Name");
           dt.Columns.Add("Department");
           dt.Rows.Add("John Alter", "D01");
           dt.Rows.Add("Prakash Kiran", "D01");
           dt.Rows.Add("Steffy Cold", "D01");
           dt.Rows.Add("Julia Roberts", "D02");
           dt.Rows.Add("Candy Mitchel", "D02");
           dt.Rows.Add("Monika Diesel", "D03");
           var departments = dt.AsEnumerable().Select(k => k.Field<string>("Department")).Distinct().OrderBy(k=>k).ToArray();

           foreach (string dept in departments)
           {
               int i = 0;
               foreach (DataRow row in dt.Rows)
               {
                   if (row["Department"].ToString() == dept) {
                       if (i > 0) {
                           row["Department"] = "";
                       }
                       i++;
                   }
               }
           }


That code was mine and as we established in our comment discussion, replacing the == operator by the .Equals method works.

For future reference, here's why that worked:
The return type of x["Department"] and dt.Rows[i]["Department"] is object (and not string, because the table can contain other data as well). When == is used on object, this is the equivalent of calling Object.ReferenceEquals[^] - here it doesn't matter that the actual type is a string, Object.ReferenceEquals is still used (and that's what I forgot when writing the original code). If you use .Equals however, then it will use String.Equals[^] (which is what you want), because you have a string and the String type overrides Object.Equals.


这篇关于删除数据表的重复列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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