使用Linq查找重复项,但获得整个记录 [英] Using Linq to find duplicates but get the whole record

查看:121
本文介绍了使用Linq查找重复项,但获得整个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用此代码

    var duplicates = mg.GroupBy(i => new { i.addr1, i.addr2 })
                    .Where(g => g.Count() > 1)
                    .Select(g=>g.Key);
    GridView1.DataSource = duplicates;
    GridView1.DataBind();

在基于addr1和addr2的表中查找并列出重复项.这段代码的唯一问题是,当我实际上要显示记录的所有字段时,它只给我一对重复的addr1和addr2. (所有字段,例如ID,addr1,addr2,城市,州...)

to find and list the duplicates in a table based on addr1 and addr2. The only problem with this code is that it only gives me the pair of addr1 and addr2 that are duplicates when i actually want to display all the fields of the records. ( all the fields like ID, addr1, addr2, city, state...)

有什么想法吗?

推荐答案

要获取所有值,可以在IGrouping

To get all values, you can use ToList() on IGrouping

var duplicates = mg.GroupBy(i => new { i.addr1, i.addr2 })
                   .Where(g => g.Count() > 1)
                   .Select(g => new {g.Key, Values = g.ToList()});

这篇关于使用Linq查找重复项,但获得整个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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