fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. class DateItem {
  6. public DateTime? Date;
  7. public int OtherField;
  8. // other fields
  9. }
  10.  
  11. public class Test
  12. {
  13. public static void Main()
  14. {
  15. var list = new List<DateItem>();
  16. list.Add(new DateItem() { Date = new DateTime(2012, 01, 12), OtherField=500 });
  17. list.Add(new DateItem() { Date = new DateTime(2012, 01, 12), OtherField=700 });
  18. list.Add(new DateItem() { Date = new DateTime(2012, 01, 15), OtherField=900 });
  19. list.Add(new DateItem() { Date = new DateTime(2012, 01, 15), OtherField=1100 });
  20. list.Add(new DateItem() { Date = new DateTime(2012, 01, 27), OtherField=2000 });
  21.  
  22. var dups = list.Select((Item,Index) => new{Item,Index})
  23. .GroupBy(x => x.Item.Date)
  24. .Where(g => g.Count() > 1);
  25. foreach(var dup in dups)
  26. {
  27. foreach (var nullable in dup.OrderBy(x => x.Item.Date).Skip(1))
  28. {
  29. list[nullable.Index].Date = null; // assuming DateTime?
  30. }
  31. }
  32.  
  33. var result = String.Join(Environment.NewLine, list.Select(i => string.Format("{0} {1}",
  34. i.Date.HasValue ? i.Date.Value.ToShortDateString() : "null", i.OtherField)).ToArray());
  35. Console.Write(result);
  36. }
  37. }
Success #stdin #stdout 0.06s 37248KB
stdin
Standard input is empty
stdout
1/12/2012 500
null 700
1/15/2012 900
null 1100
1/27/2012 2000