Error in CslaQueryProvider?

Error in CslaQueryProvider?

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


ajj3085 posted on Thursday, September 25, 2008

Hi,

I'm trying to write a business rule in a child object, which scans its peers in the parent collection for duplicate location numbers.  I have the following rule method:

        private static bool EnsureUnique( Location target, RuleArgs e ) {
            bool result;
            Locations parent;
            string location;

            parent = (Locations)target.Parent;

            if ( parent == null ) {
                result = true;
            }
            else {
                location = target.ReadProperty( LocationNumberProperty );

                try {
                    result =
                        (
                            from others in parent
                            where others.locationId != target.locationId
                            select others
                        ).Any();
                }
                catch ( Exception ex ) {
                    throw ex;
                }

                if ( !result ) {
                    e.Description =
                        string.Format(
                            Properties.Resources.LocationsNotUnique,
                            location
                        );
                }
            }

            return result;
        }

Now, obviously the Linq query in the try block isn't the final rule method... but I'm curious as to why this is throwing an exception.

ArgumentOutOfRangeException:          Message    "Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"    string


stack trace:
"   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)\r\n   at System.ThrowHelper.ThrowArgumentOutOfRangeException()\r\n   at System.Collections.Generic.List`1.get_Item(Int32 index)\r\n   at System.Collections.ObjectModel.Collection`1.get_Item(Int32 index)\r\n   at Csla.LinqBindingList`1.CopyTo(T[] array, Int32 arrayIndex)\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at Csla.Linq.CslaQueryProvider`2.Execute(Expression expression)\r\n   at Csla.Linq.CslaQueryProvider`2.Execute[TResult](Expression expression)\r\n   at System.Linq.Queryable.Any[TSource](IQueryable`1 source)\r\n   at MedAssociates.Inventory.Location.EnsureUnique(Location target, RuleArgs e) in C:\\source\\MedInventory.Business\\MedInventory.Business\\Location.cs:line 118"

So... anyone have any ideas?

AaronErickson replied on Thursday, September 25, 2008

Can you tell me what version you are using? I just wrote the following test, attempting to re-create the issue, which passes:

[TestMethod]
public void TestQueryProviderAnyOnEmpty()
{
CollectionExtendingIQueryable random = new CollectionExtendingIQueryable();

var filteredResult = from r in random
where r.SomeVal select r;

bool hasNot = filteredResult.Any();

Assert.IsFalse(hasNot);
}

ajj3085 replied on Thursday, September 25, 2008

I'm on 3.5.1.

Of course now, after lunch, it seems to be working fine now.

Copyright (c) Marimer LLC