fork(1) download
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using System.Collections.Generic;
  8.  
  9. namespace NetTest
  10. {
  11.  
  12. class Test
  13. {
  14. class Item {
  15. public int Id { get; set; }
  16. public string Name { get; set; }
  17. public override string ToString() {
  18. return $"Id={Id} Name={Name}";
  19. }
  20. }
  21. static void Main(string[] args) {
  22. var schedules = new List<Item>{
  23. new Item { Id=1, Name = "S" },
  24. new Item { Id=2, Name = "P" },
  25. new Item { Id=3, Name = "X" },
  26. new Item { Id=4, Name = "X" },
  27. new Item { Id=5, Name = "P" },
  28. new Item { Id=6, Name = "P" },
  29. new Item { Id=7, Name = "P" },
  30. new Item { Id=8, Name = "S" }
  31. };
  32. IDictionary<string,int> count = new Dictionary<string,int>();
  33. var groups = schedules
  34. .Select((s, i) => new {
  35. Item = s
  36. , Index = i
  37. })
  38. .GroupBy(p => {
  39. var name = p.Item.Name;
  40. int current;
  41. if (!count.TryGetValue(name, out current)) {
  42. current = 0;
  43. count.Add(name, current);
  44. }
  45. count[name] = current + 1;
  46. return new { Name = name, Order = current - p.Index };
  47. })
  48. .Select(g => g.ToList())
  49. .Where(g => g.Count > 1)
  50. .ToList();
  51. foreach (var g in groups) {
  52. Console.WriteLine("-----");
  53. foreach (var s in g) {
  54. Console.WriteLine(s);
  55. }
  56. }
  57. }
  58.  
  59. }
  60.  
  61. }
  62.  
Success #stdin #stdout 0.04s 16508KB
stdin
Standard input is empty
stdout
-----
{ Item = Id=3 Name=X, Index = 2 }
{ Item = Id=4 Name=X, Index = 3 }
-----
{ Item = Id=5 Name=P, Index = 4 }
{ Item = Id=6 Name=P, Index = 5 }
{ Item = Id=7 Name=P, Index = 6 }