简体   繁体   English

在数据表中查找重复项然后比较重复项

[英]find duplicates in a datatable then compare duplicates

I have a datatable the has duplicate lines.我有一个有重复行的数据表。 I need to get the duplicates and compare the duplicate lines for the best value in certain columns.我需要获取重复项并比较重复行以获得某些列中的最佳值。

DataTable dt = new DataTable();

dt.Rows.Add(1, "Test1", "584", 12);
dt.Rows.Add(2, "Test2", "32", 123);
dt.Rows.Add(3, "Test3", "425", 54);
dt.Rows.Add(4, "Test1", "4", 755);
dt.Rows.Add(5, "Test5", "854", 879);
dt.Rows.Add(6, "Test2", "1", null);
dt.Rows.Add(7, "Test2", "999", 3);

Notice Test 1 and 2 have duplicates.注意测试 1 和 2 有重复项。

(1, "Test1", "584", 12)
(4, "Test1", "4", 755)

(2, "Test2", "32", 123)
(6, "Test2", "1", null)
(7, "Test2", "999", 3)

Now that I have the duplicates.现在我有了重复项。 I need to make one line that has the best values.我需要制作一条具有最佳价值的线。 New datatable should show:新数据表应显示:

Test1 = "Test1", "584", 755
Test2 = "Test2", "999", 123
Test3 = "Test3", "425", 54
Test5 = "Test5", "854", 879
//GroupBy(x => x[1]) = groupby the second column
//Where(x => x.Count() > 1) = only get groups that have a count greater than 1, so duplicates
var duplicates = dt.Rows.OfType<DataRow>().GroupBy(x => x[1]).Where(x => x.Count() > 1).ToList();

//enumerate all duplicates
foreach (var duplicate in duplicates)
{
    //enumerate each row of the duplicate
    foreach (var dataRow in duplicate)
    {
        //do something…
        //I don't know your rules why a row is better than the other, so that part you have to figure out yourself, or extend your question
    }
}

Maybe you are looking for this:也许你正在寻找这个:

DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Text", typeof(string));
dt.Columns.Add("Value1", typeof(string));
dt.Columns.Add("Value2", typeof(int));

dt.Rows.Add(1, "Test1", "584", 12);
dt.Rows.Add(2, "Test2", "32", 123);
dt.Rows.Add(3, "Test3", "425", 54);
dt.Rows.Add(4, "Test1", "4", 755);
dt.Rows.Add(5, "Test5", "854", 879);
dt.Rows.Add(6, "Test2", "1", null);
dt.Rows.Add(7, "Test2", "999", 3);

var duplicates = dt.Rows.OfType<DataRow>().GroupBy(x => x[1]).Where(x => x.Count() > 1).ToList();

//get the current highestId (first column) so that when we remove duplicates and a new row the new row will get the next available id
var highestId = dt.Rows.OfType<DataRow>().Max(x => (int)x[0]);

//enumerate all duplicates
foreach (var duplicate in duplicates)
{
    //get the highest value of each column
    var newId = ++highestId;
    var newText = duplicate.Key;
    var newValue1 = duplicate.Max(x => x[2]); //this does a string comparison, instead of a numeric one, this means that for example that 2 is bigger then 10

    // use this if you need numeric comparison
    var newValue1AsNumeric = duplicate.Select(x =>
    {
        if (int.TryParse(Convert.ToString(x[2]), out var value))
            return value;

        return (int?)null;
    }).OfType<int>().Max(); 

    var newValue2 = duplicate.Select(x => x[3]).OfType<int>().Max();

    //enumerate each row of the duplicate
    foreach (var dataRow in duplicate)
        dt.Rows.Remove(dataRow);

    dt.Rows.Add(newId, newText, newValue1, newValue2);
}

You can see code in action here: https://dotnetfiddle.net/rp1DUc您可以在此处查看实际代码: https://dotnetfiddle.net/rp1DUc

Use DataTable.AsEnumerable() // LINQ Then use GroupBy(), // LINQ filter records, Process Them, create new DataTable / Remove non required records from same data table Done.使用 DataTable.AsEnumerable() // LINQ 然后使用 GroupBy(), // LINQ 过滤记录,处理它们,创建新的 DataTable / 从同一数据表中删除不需要的记录 Done。

I named your columns to make things a bit easier:我为您的专栏命名以使事情变得更容易:

DataTable dt = new DataTable();
dt.Columns.Add("id", Type.GetType("System.Int32"));
dt.Columns.Add("group", Type.GetType("System.String"));
dt.Columns.Add("first", Type.GetType("System.String"));
dt.Columns.Add("second", Type.GetType("System.Int32"));

dt.Rows.Add(1, "Test1", "584", 12);
dt.Rows.Add(2, "Test2", "32", 123);
dt.Rows.Add(3, "Test3", "425", 54);
dt.Rows.Add(4, "Test1", "4", 755);
dt.Rows.Add(5, "Test5", "854", 879);
dt.Rows.Add(6, "Test2", "1", null);
dt.Rows.Add(7, "Test2", "999", 3);

You can then group and find the maximums (assuming that is what you mean by 'best') using Linq:然后,您可以使用 Linq 分组并找到最大值(假设这就是“最佳”的意思):

var group = dt.AsEnumerable().GroupBy(row => row.Field<string>("group")).Select(g => new
{
    group = g.Key,
    first = g.Max(row => int.Parse(row.Field<string>("first"))).ToString(),
    second = g.Max(row => row.Field<int?>("second") ?? 0)
}).ToList();

This gives you a list that matches your desired output.这将为您提供与您所需的 output 匹配的列表。 I have assumed that a null value should be considered to be a value of 0. You can then put the values back into your original DataTable:我假设null值应被视为 0。然后您可以将这些值放回原始数据表中:

dt.Clear();
var rowCount = 1;
foreach (var x in group)
    dt.Rows.Add(rowCount++, x.group, x.first, x.second);

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

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