fork download
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class Test
  7. {class Car
  8. {
  9. public int CarId;
  10. public int Location ;
  11. }
  12.  
  13. static int TotalCycles=0;
  14.  
  15. class Cycle
  16. {
  17. public int CycleNum;
  18. public List<Car> Cars;
  19. public Cycle ()
  20. {
  21. CycleNum=(++TotalCycles);
  22. }
  23.  
  24. }
  25.  
  26. class MyEqualityComparer : IEqualityComparer<Car>
  27. {
  28. public bool Equals(Car item1, Car item2)
  29. {
  30. if(object.ReferenceEquals(item1, item2))
  31. return true;
  32. if(item1 == null || item2 == null)
  33. return false;
  34. return item1.CarId.Equals(item2.CarId) ;
  35. }
  36.  
  37.  
  38. public int GetHashCode(Car car)
  39. {
  40. //Check whether the object is null
  41. if (Object.ReferenceEquals(car, null)) return 0;
  42.  
  43.  
  44.  
  45. //Get hash code for the Code field.
  46. int hashcarCode = car.CarId.GetHashCode();
  47.  
  48. //Calculate the hash code for the car.
  49. return hashcarCode;
  50. }
  51. }
  52. public static void Main()
  53. {
  54. List<Cycle> LstCyclces = new List<Cycle>();
  55. Cycle cycle =null;
  56.  
  57. cycle = new Cycle();//cycle 1
  58. cycle.Cars = new List<Car>();
  59. cycle.Cars.Add(new Car {CarId=1 , Location=40});
  60. cycle.Cars.Add(new Car {CarId=2 , Location=21});
  61. cycle.Cars.Add(new Car {CarId=3 , Location=5});
  62. cycle.Cars.Add(new Car {CarId=4 , Location=15});
  63. LstCyclces.Add(cycle);
  64.  
  65. cycle = new Cycle();//cycle2
  66. cycle.Cars = new List<Car>();
  67. cycle.Cars.Add(new Car {CarId=1 , Location=40}); //same location
  68. cycle.Cars.Add(new Car {CarId=2 , Location=57});//changed location
  69. cycle.Cars.Add(new Car {CarId=3 , Location=100});//changed location
  70. cycle.Cars.Add(new Car {CarId=4 , Location=7});//changed location
  71. cycle.Cars.Add(new Car {CarId=7 , Location=2});//new attended ( vs previous cycle)
  72. LstCyclces.Add(cycle);
  73.  
  74. cycle = new Cycle();//cycle3
  75. cycle.Cars = new List<Car>();
  76. cycle.Cars.Add(new Car {CarId=1 , Location=40}); //same
  77. cycle.Cars.Add(new Car {CarId=2 , Location=5});//changed Location
  78. cycle.Cars.Add(new Car {CarId=4 , Location=1});//changed Location
  79. cycle.Cars.Add(new Car {CarId=9 , Location=7});//new attended ( vs previous cycle)
  80. LstCyclces.Add(cycle);
  81.  
  82.  
  83. var cycleToCheck=2;
  84. var requestedCycleCars = LstCyclces.Where(c=>c.CycleNum==cycleToCheck).SelectMany(c=>c.Cars);
  85. var previousCycleCars = LstCyclces.Where(c=>c.CycleNum==cycleToCheck-1).SelectMany(c=>c.Cars);
  86.  
  87. //requestedCycleCars.Intersect(previousCycleCars,new MyEqualityComparer());
  88.  
  89. }
  90. }
Success #stdin #stdout 0.01s 33656KB
stdin
Standard input is empty
stdout
Standard output is empty