[英]Get duplicates from C# List<object>
I have the following List definition: 我有以下List定义:
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. entitiesList的accountNumbers中有重复项。 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.
我想找到重复的accountNumbers,在locationGuids上执行一个操作,其创建日期不是重复项的最新创建日期。 How can I manipulate the list to get only for the duplicates the accountNumber, most recently created locationGuid and the (older) locationGuids?
如何操作列表以仅获取重复项accountNumber,最近创建的locationGuid和(较旧的)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
}
duplicates = entitiesList.GroupBy(e => e.accountNumber)
.Where(g => g.Count() > 1)
.Select(g => g.OrderByDescending(x => x.createdon));
List<ListItem> entitiesList = new List<ListItem>();
var filtered = entitiesList.GroupBy(x => x.accountNumber).Where(g => g.Count() > 1).ToList().OrderByDescending(x => x.createdon);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.