Guest User

Untitled

a guest
Mar 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. List<int> agents = taskdal.GetOfficeAgents(Branches.aarhusBranch);
  2. if (lastAgentIDAarhus != -1)
  3. {
  4. int index = agents.IndexOf(lastAgentIDAarhus);
  5. if (agents.Count > index + 1)
  6. {
  7. lastAgentIDAarhus = agents[index + 1];
  8. }
  9. else
  10. {
  11. lastAgentIDAarhus = agents[0];
  12. }
  13. }
  14. else
  15. {
  16. lastAgentIDAarhus = agents[0];
  17. }
  18.  
  19. lastAgentIDAarhus = agents[index == -1 ? 0 : index % agents.Count];
  20.  
  21. public static T NextOf<T>(this IList<T> list, T item)
  22. {
  23. var indexOf = list.IndexOf(item);
  24. return list[indexOf == list.Count - 1 ? 0 : indexOf + 1];
  25. }
  26.  
  27. List<string> names = new List<string>();
  28.  
  29. names.Add("jonh");
  30. names.Add("mary");
  31.  
  32. string name = String.Empty;
  33.  
  34. name = names.NextOf(null); //name == jonh
  35.  
  36. name = names.NextOf("jonh"); //name == mary
  37.  
  38. name = names.NextOf("mary"); //name == jonh
  39.  
  40. public static IEnumerable<T> AsCircularEnumerable<T>(this IEnumerable<T> enumerable)
  41. {
  42. var enumerator = enumerable.GetEnumerator();
  43. if(!enumerator.MoveNext())
  44. yield break;
  45.  
  46. while (true)
  47. {
  48. yield return enumerator.Current;
  49. if(!enumerator.MoveNext())
  50. enumerator = enumerable.GetEnumerator();
  51. }
  52. }
  53.  
  54. var agents = new List<int> {1, 2, 3, 4, 123, 234, 345, 546};
  55.  
  56. foreach(var i in agents.AsCircularEnumerable())
  57. {
  58. Console.WriteLine(i);
  59. }
  60.  
  61. public static T Next<T>(this IList<T> list, T item)
  62. {
  63. var nextIndex = list.IndexOf(item) + 1;
  64.  
  65. if (nextIndex == list.Count)
  66. {
  67. return list[0];
  68. }
  69.  
  70. return list[nextIndex];
  71. }
  72.  
  73. List<int> agents = taskdal.GetOfficeAgents(Branches.aarhusBranch);
  74. if (lastAgentIDAarhus != -1)
  75. {
  76. int index = agents.IndexOf(lastAgentIDAarhus);
  77. lastAgentIDAarhus = (agents.Count > index + 1 ? agents[index + 1] : agents[0]);
  78. }
  79. else
  80. {
  81. lastAgentIDAarhus = agents[0];
  82. }
  83.  
  84. public static class ListExtensions
  85. {
  86. public static TType Next<TType>(this IList<TType> list, TType item)
  87. {
  88. if (list == null) return default(TType);
  89.  
  90. var itemIndex = list.IndexOf(item);
  91. if (itemIndex < 0) return list.FirstOrDefault();
  92.  
  93. var nextIndex = itemIndex + 1;
  94.  
  95. return nextIndex >= list.Count
  96. ? list.FirstOrDefault()
  97. : list[nextIndex];
  98. }
  99. }
Add Comment
Please, Sign In to add comment