使用LINQ查找列表中的重复项 [英] Finding out duplicates in the list using LINQ

查看:60
本文介绍了使用LINQ查找列表中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个客户列表。


ex: -


标识              名称               电子邮件                    
地址


1                                        a@a.com 功能;               
abc


2                  B                    
b@b.com               ;     abc


3                  C                    
c@c.com              ;     abc


4                   D                     
d@d.com              ;    abc


1                  A                    
a@a.com              ;    abc


我应该做的是使用LINQ将唯一项目和重复项目分开,i:e;


我需要准备2客户列表如下所示,


重复列表: -


ID          ;     名称               电子邮件                    
地址


1                                        a@a.com 功能;               
abc


1                  A                    
a@a.com              ;     abc


唯一清单: -


ID             ;  名称               电子邮件                    
地址


2                  B                    
b@b.com               ;     abc


3                  C                    
c@c.com              ;     abc


4                   D                     
d@d.com              ;    abc


如何使用LINQ实现这一目标?


请帮忙,


先谢谢。

解决方案

你可以通过将Distinct方法与except方法结合使用来实现这一点


http://msdn.microsoft.com/en-us/library/system.linq。 enumerable.distinct.aspx


http:// msdn.microsoft.com/en-us/library/bb300779.aspx


但是存在性能问题......


作为替代方案,您可以创建列表A和B(这是元素所在的位置),您可以通过重复字段对其进行排序并对其进行迭代,如果下一个值不相同,我们将记录移动到A,最后得到非重复在列表A中,并在列表B中重复,在我头脑中的顶部
之外,这是我能想到的更清洁的东西,也许还有更好的东西。


问候


Hi All,

Im having a list of customers.

for ex:-

Id               Name                Email                     Address

1                  A                     a@a.com                 abc

2                  B                     b@b.com                 abc

3                  C                     c@c.com                 abc

4                  D                     d@d.com                abc

1                  A                     a@a.com                abc

What im supposed to do here is separate the unique items and duplicate items using LINQ, i:e;

i need to prepare 2 lists of customers as shown below,

Duplicate list:-

Id               Name                Email                     Address

1                  A                     a@a.com                 abc

1                  A                     a@a.com                 abc

Unique List:-

Id               Name                Email                     Address

2                  B                     b@b.com                 abc

3                  C                     c@c.com                 abc

4                  D                     d@d.com                abc

How to achieve this using LINQ?

Please help,

Thanks in Advance.

解决方案

You could achieve this by using the Distinct method in combination with the except method

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.distinct.aspx

http://msdn.microsoft.com/en-us/library/bb300779.aspx

However there is a performance issue...

As an alternative you create list A and B(this is where the elements are), you order it by the repeat field and iterate it, if next value is not the same we move record to A, at the end you get non repeats in list A and repeats in list B, off the top of my head that is the cleaner thing I can think of, perhaps there is something better.

Regards


这篇关于使用LINQ查找列表中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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