Get duplicates from C # list <object>

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 List object. I want to find a duplicate accountNumbers, perform an action on locationGuids with a createdon date that is not the most recent duplicate date created. How can I manipulate the list to only duplicate accountNumber, the newly created locationGuid and (older) locationGuids?

+2
source share
3 answers
 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 } 
+2
source
 duplicates = entitiesList.GroupBy(e => e.accountNumber) .Where(g => g.Count() > 1) .Select(g => g.OrderByDescending(x => x.createdon)); 
+2
source
  List<ListItem> entitiesList = new List<ListItem>(); var filtered = entitiesList.GroupBy(x => x.accountNumber).Where(g => g.Count() > 1).ToList().OrderByDescending(x => x.createdon); 
0
source

All Articles