Steve Lam
Steve Lam

Reputation: 509

Linq with group by in datatable

I have a datatable with "year" and "month" columns

Year        Month
2012        1
2012        2
2012        1

Now I want to check if there is any duplicated values, how can I do this? Thanks!

Upvotes: 0

Views: 1394

Answers (2)

Steve Lam
Steve Lam

Reputation: 509

Thanks for you guys tips! I have modified it into vb code as below

Return new_dt.AsEnumerable().GroupBy(Function(row) New With { _
                                       Key .Year = row("year"), _
                                       Key .Month = row("month") _
                                     }).Where(Function(g) g.Count > 1).Count > 0

Upvotes: 0

cuongle
cuongle

Reputation: 75306

 return dataTable.AsEnumerable()
            .Select(row => new {Year = row[0], Month = row[1]})
            .GroupBy(x => new {x.Year, x.Month})
            .Any(g => g.Count()>1);

Upvotes: 1

Related Questions