1

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.

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
User
  • 47
  • 6

2 Answers2

1

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);
    ...
Michał Komorowski
  • 6,198
  • 1
  • 20
  • 24
  • 1
    Thanks for reply. already we used the input as IEnumerable type .We convert that into AsQueryable for our process. Here you told to again convert back to IEnumerable as ToList(). Also already we tried the above approach and we need to know why the entry of AsQueryable throws exception and we need to update our customer with valid reason. – User Sep 18 '14 at 13:44
  • @user2826004 If you want to know the exact reason why it doesn't work you can download the source code of MyToolkit and debug it. – Michał Komorowski Sep 19 '14 at 07:17
0

Have you istancied the collection first?

Sid
  • 14,176
  • 7
  • 40
  • 48
  • I instantiated the collection like SampleEntries=GetSampleData(); private ObservableCollectionView GetSampleData() { ObservableCollectionView sampleSource = new ObservableCollectionView(); for (int i = 1; i <= 50; i++) { Person person = new Person {Id = i, Name = "Person " + i, Age = i}; sampleSource.Add(person); } return sampleSource; } – User Sep 16 '14 at 13:13