从C#List中获取重复项< object> [英] Get duplicates from C# List<object>

查看:789
本文介绍了从C#List中获取重复项< object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下列表定义:

 类列表项
{
公众诠释了accountNumber {得到;组; }
公众的Guid locationGuid {搞定;组; }
公众的DateTime createdon {搞定;组; }
}
类节目
{
静态无效的主要(字串[] args)
{
名单,LT;列表项> entitiesList =新的List<&列表项GT;();
//一些代码来填充entitiesList
}
}

有在entitiesList的accountNumbers重复。我想找到重复的accountNumbers,做与不重复的最新createdon日期createdon日期locationGuids的动作。如何操作列表中只拿到了了accountNumber,最近创建locationGuid和(以上)locationGuids重复的?


解决方案

 列表<&列表项GT; entitiesList =新的List<&列表项GT;(); 
//一些代码来填充列表
变种重复= entitiesList.OrderByDescending(E => e.createdon)
.GroupBy(E => e.accountNumber)
。凡(E => e.Count()> 1)
。选择(G =>新建
{
MostRecent = g.FirstOrDefault(),
其他= g.Skip(1).ToList()
});

的foreach(一式两份VAR项)
{
列表项mostRecent = item.MostRecent;
名单,LT;列表项>其他= item.Others;
//做的东西跟别人
}


I have the following List definition:

class ListItem
{
    public int accountNumber { get; set; }
    public Guid locationGuid { get; set; }
    public DateTime createdon { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        List<ListItem> entitiesList = new List<ListItem>();
        // Some code to fill the entitiesList
    }
}

There are duplicates in the accountNumbers of the entitiesList. I want to find the duplicate accountNumbers, do an action on the locationGuids with a createdon date that is not the most recent createdon date of the duplicates. How can I manipulate the list to get only for the duplicates the accountNumber, most recently created locationGuid and the (older) locationGuids?

解决方案

List<ListItem> entitiesList = new List<ListItem>();
//some code to fill the list
var duplicates = entitiesList.OrderByDescending(e => e.createdon)
                    .GroupBy(e => e.accountNumber)
                    .Where(e => e.Count() > 1)
                    .Select(g => new
                    {
                        MostRecent = g.FirstOrDefault(),
                        Others = g.Skip(1).ToList()
                    });

foreach (var item in duplicates)
{
    ListItem mostRecent = item.MostRecent;
    List<ListItem> others = item.Others;
    //do stuff with others
}

这篇关于从C#List中获取重复项&lt; object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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