使用linq C#从数据表中选择整个重复行 [英] Select entire duplicate row from datatable with linq c#

查看:58
本文介绍了使用linq C#从数据表中选择整个重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以得到它来返回单列,但是我似乎无法弄清楚如何获得它来给我整行而不是仅仅给列值.

I can get it to return the single column, but I can't seem to figure out how to get it to give me the entire row instead of just the column values.

帮助!

它应该返回的字段是VDMList和Table.

the fields it should return are, VDMList and Table.

var duplicates = dt.AsEnumerable()
    .Select(dr => dr.Field<string>("VMDList"))
    .GroupBy(x => x)
    .Where(g => g.Count() > 1)
    .Select(g => g.Key)
    .ToList();


columns of DT Table, VDMList
output is
4  | 02,2
12 | 03,3
15 | 02,2

推荐答案

以下查询将为重复的VMDList行返回group中的first项.请注意,没有选择first项目的条件.它可以是任何random行.

Following query will return first item in the group for rows with duplicate VMDList. Note that there is no criteria for selecting the first item. It can be any random row.

var duplicates = dt.AsEnumerable()
    .GroupBy(dr => dr.Field<string>("VMDList"))
    .Where(g => g.Count() > 1)
    .Select(g => g.First())
    .ToList();

要返回所有重复的行,请使用 SelectMany

To return all rows that have duplicates, Use SelectMany

var allDuplicates = dt.AsEnumerable()
    .GroupBy(dr => dr.Field<string>("VMDList"))
    .Where(g => g.Count() > 1)
    .SelectMany(g => g)
    .ToList();

这篇关于使用linq C#从数据表中选择整个重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆