NullReference exception when adding SortDescriptor to Datasource

Enumerable I'm using a SfListView which is connected to a DataSource, the DataSource.Source is an IEnumerable<MyModel> this all seems to work fine and display the data as expected and if the underlying database should change, calling DataSource.Refresh() does the job.

The problem starts when I try to sort the DataSource:

            DataSource.SortDescriptors.Clear();
            DataSource.SortDescriptors.Add(new SortDescriptor
            {
                PropertyName = "DataRow.Name",
                Direction = _sortDirection
            });

throws a NullReferenceException on the Add method.

Now if I change the DataSource.Source to a List<> or ObservableCollection<>, the sorting code as above works fine. The problem then is when I call DataSource.Refresh, the data does not update as the underlying List/Collection has not changed.

Is it not possible to sort the DataSource when the Source is IEnumerable , is it recommended i perform the sort in the linq ?

5 Replies 1 reply marked as answer

LN Lakshmi Natarajan Syncfusion Team November 3, 2020 09:24 AM UTC

Hi Lee, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “NullReference exception when adding SortDescriptor to Datasource” from our side. We would like to inform you that we have checked the reported scenario from our side. But we are unable to reproduce the scenario in our sample. We have attached the tested sample in the following link, 
 
 
Additional information: 
Tested device: 
Android - Samsung Tablet 7.1 – API 15 
iOS – iPhone 12 iOS 14.1 
UWP 
SfListView version: 18.3.0.44 
Xamarin Forms: 4.8.0.1451 
Xamarin: 16.7.000.456 
Xamarin Designer: 16.7.0.495 
Xamarin Templates: 16.7.85 
Xamarin Android SDK: 11.0.2.0 
Xamarin.iOS: 14.2 
 
Please check our sample and let us know if you still facing the same issue? if not, please modify our sample and revert us back with the following details which would be helpful for us to check on it and provide you the solution as soon as possible, 
  • Share the issue reproducible sample
  • Share ListView and DataSource code snippets
  • Share the stack trace of the crash
  • Share device configuration details
  • Share Syncfusion and Xamarin.Forms version
 
Lakshmi Natarajan 
  
 



LH Lee Henderson November 3, 2020 06:19 PM UTC

I'm not sure if you understand fully, so I've updated your example to show the issue.


I know the easy fix here is to create a list from the IEnumerable but in doing so I lose the ability to call DataSource.Refresh() and have it pull the updated data. I don't want to have to find objects in lists and replace them or create a new list again.


LN Lakshmi Natarajan Syncfusion Team November 4, 2020 09:12 AM UTC

Hi Lee, 
 
Thank you for the update. 
 
We have checked the provided sample and we could reproduce the reported scenario at our side. We would like to inform you that you can overcome the reported scenario by setting the SourceType property as the model class type. Please refer the following code snippets for more reference, 
 
ViewModel 
public class ViewModel 
{ 
    public IEnumerable<Contacts> Contacts() 
    { 
        return CustomerNames.Select(name => new Contacts() 
        { 
            ContactName = name 
        }); 
    } 
 
    public ViewModel() 
    { 
        DataSource = new DataSource 
        { 
            Source = Contacts(), 
            SourceType = typeof(Contacts) 
        }; 
    } 
... 
 
Please refer our user guidance document regarding the same from the following link, 
 
Please let us know if you need further assistance. 
 
Lakshmi Natarajan 
 



LH Lee Henderson November 4, 2020 02:49 PM UTC

Thank you for highlighting this.

I saw this in the docs but its not totally clear. Also, maybe you should consider a better error than allowing a Null Reference to be thrown as I was unable to trace this to anything meaningful and wasted a days work.



LN Lakshmi Natarajan Syncfusion Team November 5, 2020 12:13 PM UTC

Hi Lee, 
 
Thank you for the update. 
 
We would like to inform you that when using IEnumerable, we could not get the run time property of the IEnumerable and the source type is null. Hence, we suggested you to set the SourceType property as the base type. 
 
Please let us know if you need further assistance. 
 
Lakshmi Natarajan 


Marked as answer
Loader.
Up arrow icon