Return number of cells according to parameter of a datatable

Asked

Viewed 60 times

1

Do I need to take duplicate cells from a particular column, is there any language function that returns these duplicate cells to me? Example:

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

would have to return me the valor 1,2 or the lines these values are in column-1

1 answer

2


Using System.Linq;

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

Full 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:

http://rextester.com/OYHGL73505

Browser other questions tagged

You are not signed in. Login or sign up in order to post.