Find missing elements
-
I have a collection of objects that all of a property
Index
. I am doing some stuff to grab certains ones and combine them. Now I would like to find any gaps. So I have say 4 elements in a collection with indecies 3,4,5,8,9 since 6 and 7 are missing I would like to know that and then add them from a different collection. Any ideas? I was thinking if I could find the 5 I could cycle up to 8. So to do this I can find the max (9) and say if the item is not the max index but it is missing a + 1. Not sure how to get that into a proper query though :sigh:Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
I have a collection of objects that all of a property
Index
. I am doing some stuff to grab certains ones and combine them. Now I would like to find any gaps. So I have say 4 elements in a collection with indecies 3,4,5,8,9 since 6 and 7 are missing I would like to know that and then add them from a different collection. Any ideas? I was thinking if I could find the 5 I could cycle up to 8. So to do this I can find the max (9) and say if the item is not the max index but it is missing a + 1. Not sure how to get that into a proper query though :sigh:Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
I have a collection of objects that all of a property
Index
. I am doing some stuff to grab certains ones and combine them. Now I would like to find any gaps. So I have say 4 elements in a collection with indecies 3,4,5,8,9 since 6 and 7 are missing I would like to know that and then add them from a different collection. Any ideas? I was thinking if I could find the 5 I could cycle up to 8. So to do this I can find the max (9) and say if the item is not the max index but it is missing a + 1. Not sure how to get that into a proper query though :sigh:Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
Hi Collin; This can be done with linq as shown in the code snippet below.
// Your values from Index property
List<int> myElements = new List<int>() { 8, 5, 9, 3, 4 };// Create all integers from min value to max value in above collection
var range = from n in Enumerable.Range(myElements.Min(), myElements.Max() - myElements.Min() + 1)
select n;// Find all missing values
List<int> result = range.Except(myElements).ToList();