Exception : Key Not Present in Dictionary

Feb 18, 2011 at 10:11 PM

Anyone else getting a "key not present in dictionary" exception?

My users are getting this Obtics exception.  Not sure how to reproduce yet.  I will post more details as they become available.

Anyone have this same exception?


Feb 18, 2011 at 10:31 PM

A 'regular' way this might happen is when you would use a LINQ statent like this  collection.Select(x => new object()) and make it observable.

Obtics uses your lambda expression both for adding new items and removing old ones. with this state ment if you would add an item I and later remove it again after the select objtics would add one object but remove another.

So; if you would do something like collection.Select(x => new object()).Distinct().OrderBy(obj => obj.GetHashCode()) the Distinct() part would introduce a Dictionary where first object a is inserted and later object b, which is not in the dictionary, will be removed.

It is therefore important that all your lambda functions are well-behaved, meaning that they should always return the same result when given the same parameters. (where same is defined by the T.Equals(T other) method)

So always when Func<X,R> lambda = .... then EqualityComparer<R>.Default.Equals(lambda(x), lamda(x)) should be true.

Anyway, if you find out, I would be very interested in how this exception happens.