I was trying to figure out how to write the proper TakeWhile syntax using lambda in the snippet below, so i can get this filtering to work and return the filtered Dictionary. Can anyone here help? Thanks in advance..
List<KeyValuePair<int, int>> myList = myHashList.ToList();
myList.Sort( (x,y) => x.Value.CompareTo(y.Value) );
var temp = myList.TakeWhile(x => x.Value >= keyToFind);
Dictionary<int, int> outDict = temp.ToDictionary(y => y.Key, y => y.Value);
Let me also mention that here, myHashList is a Dictionary (int,int) with n integer values and integer keys. Ok, here the lambda I am using myList.TakeWhile(x => x.Value >= KeyToFind) is executing just once. That is, x during run-time holds only first record and the comparison is done once and temp has 0 records, although after the 2nd line, myList has n records. But at run-time myList is not in context when the lambda comparison is done. But again, when execution comes back to 4th line, myList is back in context but temp has always 0 records. TakeWhile is not working. Do I need to correct the lambda?
Ok, adding the answer myself here. It was a silly thing in fact. The lambda should have been (x => keyToFind >= x.Value) instead of the other way round because always my list started from Value 0 sorted in ascending order and thus would never be >= keyToFind (which again should have been ValueToFind instead of keyToFind as i intended to find Value and not Key), so thanks again!