Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<int> agents = taskdal.GetOfficeAgents(Branches.aarhusBranch);
- if (lastAgentIDAarhus != -1)
- {
- int index = agents.IndexOf(lastAgentIDAarhus);
- if (agents.Count > index + 1)
- {
- lastAgentIDAarhus = agents[index + 1];
- }
- else
- {
- lastAgentIDAarhus = agents[0];
- }
- }
- else
- {
- lastAgentIDAarhus = agents[0];
- }
- lastAgentIDAarhus = agents[index == -1 ? 0 : index % agents.Count];
- public static T NextOf<T>(this IList<T> list, T item)
- {
- var indexOf = list.IndexOf(item);
- return list[indexOf == list.Count - 1 ? 0 : indexOf + 1];
- }
- List<string> names = new List<string>();
- names.Add("jonh");
- names.Add("mary");
- string name = String.Empty;
- name = names.NextOf(null); //name == jonh
- name = names.NextOf("jonh"); //name == mary
- name = names.NextOf("mary"); //name == jonh
- public static IEnumerable<T> AsCircularEnumerable<T>(this IEnumerable<T> enumerable)
- {
- var enumerator = enumerable.GetEnumerator();
- if(!enumerator.MoveNext())
- yield break;
- while (true)
- {
- yield return enumerator.Current;
- if(!enumerator.MoveNext())
- enumerator = enumerable.GetEnumerator();
- }
- }
- var agents = new List<int> {1, 2, 3, 4, 123, 234, 345, 546};
- foreach(var i in agents.AsCircularEnumerable())
- {
- Console.WriteLine(i);
- }
- public static T Next<T>(this IList<T> list, T item)
- {
- var nextIndex = list.IndexOf(item) + 1;
- if (nextIndex == list.Count)
- {
- return list[0];
- }
- return list[nextIndex];
- }
- List<int> agents = taskdal.GetOfficeAgents(Branches.aarhusBranch);
- if (lastAgentIDAarhus != -1)
- {
- int index = agents.IndexOf(lastAgentIDAarhus);
- lastAgentIDAarhus = (agents.Count > index + 1 ? agents[index + 1] : agents[0]);
- }
- else
- {
- lastAgentIDAarhus = agents[0];
- }
- public static class ListExtensions
- {
- public static TType Next<TType>(this IList<TType> list, TType item)
- {
- if (list == null) return default(TType);
- var itemIndex = list.IndexOf(item);
- if (itemIndex < 0) return list.FirstOrDefault();
- var nextIndex = itemIndex + 1;
- return nextIndex >= list.Count
- ? list.FirstOrDefault()
- : list[nextIndex];
- }
- }
Add Comment
Please, Sign In to add comment