User
User

Reputation: 45

Getting an Exception "System.ArgumentException was unhandled by user code" while converting ObservableCollectionView into AsQueryable ()

I used ObservableCollection type of object . I tried Filter by the following code

public ObservableCollectionView<Person> SampleEntries { get; set; }


private void ApplyFilter(int age)
    {
        SampleEntries.Filter = (entry => entry.Age > age ) ;
        // converting ObservableCollection into AsQueryable
        var source = this.SampleEntries.AsQueryable();
        //Shows the value as Destination array was not long enough
        var source1 = source.OrderByDescending(x => x.Id);

    }

After applying filter , tried to sort the column , it throws the

Exception  : "System.ArgumentException was unhandled by user code" Message=Destination array was not long enough. Check destIndex and length, and the array's lower bounds.

Note: I need to know why this code is not working. We already have other ways to fix to this problem.

Update: The ObservableCollectionView class can be found in the MyToolkit library.

Upvotes: 1

Views: 2022

Answers (2)

Michał Komorowski
Michał Komorowski

Reputation: 6238

It seems to me that ObservableCollectionView was not designed to be directly used with Linq. If you just need to sort items in ObservableCollectionView in descending order you should use Order and Ascending properties. For example:

    ...
    SampleEntries.Filter = (entry => entry.Age > age ) ;
    SampleEntries.Order= x => x.Id;
    SampleEntries.Ascending = false; 
    ...

If you really need to use Linq try this:

    ...
    var source = this.SampleEntries.AsQueryable().ToList();
    var source1 = source.OrderByDescending(x => x.Id);
    ...

Upvotes: 1

Sid
Sid

Reputation: 14916

Have you istancied the collection first?

Upvotes: 0

Related Questions