Return number of cells according to parameter of a datatable

1

I need to get duplicate cells from a particular column, is there any language function that returns those duplicate cells? Example:

coluna-1  coluna-2
    1        87
    2        9
    3        12
    1        17
    2        28

I would have to return the valor 1,2 or the rows that these values are from column-1

    
asked by anonymous 06.12.2017 / 20:48

1 answer

2

Using System.Linq;

var ret = dt.AsEnumerable().GroupBy(x => x["c1"]).Where(x => x.Count() > 1);

Complete example:

var dt = new DataTable();

dt.Columns.Add("c1");
dt.Columns.Add("c2");

dt.Rows.Add(1, 87);
dt.Rows.Add(2, 9);
dt.Rows.Add(3, 12);
dt.Rows.Add(1, 17);
dt.Rows.Add(2, 28);

var ret = dt.AsEnumerable().GroupBy(x => x["c1"]).Where(x => x.Count() > 1);
var ret2 = dt.AsEnumerable().GroupBy(x => x["c2"]).Where(x => x.Count() > 1);

foreach (var value in ret)
{
    Console.WriteLine(value.Key);
}

Console.ReadLine();

You can test online:

link

    
06.12.2017 / 22:40