Bug in Csla to Linq?

Bug in Csla to Linq?

Old forum URL: forums.lhotka.net/forums/t/7486.aspx


BobK posted on Thursday, August 20, 2009

I have created a "Dynamic Editable Collection" which contains a "Dynamic Editable Root" as described in the "CSLA .NET OBJECT TEMPLATES" section of the book (pp 199-203)

I then bound the collection (through a BindingSource) to a listbox and edit controls. When an item in the listbox is selected, the edit controls update to the current record. If I edit the data and then move to another item in the list, the data is saved. All is well!

However, I wanted to implement filtering in my list, so I changed the BindingSource to be bound to the result of a Linq query:

var list = from item in col select item;

Once I am bound to the Linq result, if I click back and forth (in other words, if I re-visit a previously selected item in the list - maybe twice) in the list, I get an unhandled "ArgumentOutOfRangeException" in the EditableRootListBase::SaveItem(int index) method on the line:

item = this[index];

because index is -1.
It appears that the save is causing the item in the collection to be replaced with a clone, and so the IndexOf method returns -1.

However, when I'm bound to the base collection, this error does not occur.

RockfordLhotka replied on Sunday, August 23, 2009

What version of CSLA are you using? Some bugs have been fixed over time.

The thing to realize is that not all LINQ queries return a LinqBindingList as a result. We can only transform some queries into an LBL, other queries are rendered into an IEnumerable<T> like normal LINQ to Objects.

If you end up with an IEnumberable<T>, we can't keep the two lists in sync - and you need to realize that you really have two lists that point to some of the same objects, which is the core of the problem. This is the way LINQ works, and there's no easy solution.

If you are using a current version of CSLA (3.6.3 or higher), then you should make sure you are getting an LBL as a result of your query (use the debugger). If you are getting an LBL and are having this problem, then there may be a bug in LBL, and we'd appreciate a simple unit test style example that illustrates your failure condition.

Thank you!

Copyright (c) Marimer LLC