This project is read-only.

Another GroupJoin bug

Aug 17, 2012 at 8:33 PM

Try the following code:

    public class GroupJoinTestFixture
    {
        public class WrappedString : INotifyPropertyChanged
        {
            public WrappedString(string s)
            {
                Value = s;
            }

            public string Value { get; set; }

            #region Implementation of INotifyPropertyChanged

            public event PropertyChangedEventHandler PropertyChanged;

            #endregion
        }

        [Test]
        public void Test()
        {
            ObservableCollection outer = new ObservableCollection { new WrappedString("hello"), new WrappedString("aloha") };
            ObservableCollection inner = new ObservableCollection { new WrappedString("hello"), new WrappedString("aloha") };
            ReadOnlyContinuousCollection inner2 = inner.Where(ws => ws.Value != "");

            ReadOnlyContinuousCollection>> join =
                outer.
                    GroupJoin(inner2,
                              outerItem => outerItem.Value,
                              innerItem => innerItem.Value,
                              (outerItem, innerList) => new RichKeyValuePair>(outerItem, innerList));
            var hasInners =
                join.Where(pair => pair.Value.Count > 0);
            outer.Clear();
            outer.Add(new WrappedString("hello"));
            outer.Add(new WrappedString("aloha"));
            inner.RemoveAt(1);
        }
    }

 

After the last line (inner.RemoveAt(1)) there should be only one item in the hasInners list. But there are two.

When we clear the outer list, GroupJoinReadOnlyContinuousCollection.OnOuterReset is called. It calls RebuildAll which in turn calls RebuildAllInner. But there's no need to rebuild the _keyToInnerLookup, and doing so creates redundant entries in it.

The fix is that OnOuterReset should call RebuildAllOuter instead of RebuildAll.