获得从IEnumerable非不同的元素 [英] Get Non-Distinct elements from an IEnumerable

查看:110
本文介绍了获得从IEnumerable非不同的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫Item类。项目有一个名为ItemCode一个标识符属性,它是一个字符串。我想获得在项目列表中的所有非重复的项目列表

I have a class called Item. Item has an identifier property called ItemCode which is a string. I would like to get a list of all non-distinct Items in a list of Items.

例如:

List<Item> itemList = new List<Item>()
{
   new Item("code1", "description1"),
   new Item("code2", "description2"),
   new Item("code2", "description3"),
};



我要包含底部两个条目列表

I want a list containing the bottom two entries

如果我用

var distinctItems = itemsList.Distinct();



我得到不同的项目清单,这是伟大的,但我想那几乎相反。我可以从原来的名单中减去鲜明的名单,但将不包含所有重复,每次只是一个实例。

I get the list of distinct items which is great, but I want almost the opposite of that. I could subtract the the distinct list from the original list but that wouldn't contain ALL repeats, just one instance of each.

我有一出戏,可以' W图出优雅的解决方案。任何指针或帮助将非常感激。谢谢!

I've had a play and can't figure out an elegant solution. Any pointers or help would be much appreciated. Thanks!

我有3.5这样LINQ提供

I have 3.5 so LINQ is available

推荐答案

我的采取:

var distinctItems = 
    from list in itemsList
    group list by list.ItemCode into grouped
    where grouped.Count() > 1
    select grouped;

这篇关于获得从IEnumerable非不同的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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