Linq with group by in datatable

时间:2012-09-05 10:41:42

标签: vb.net linq

我有一个带有“年”和“月”列的数据表

Year        Month
2012        1
2012        2
2012        1

现在我想检查是否有重复值,我该怎么做?谢谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

谢谢你们的提示!我已将其修改为vb代码,如下所示

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