Forum Xamarin.Forms

Can I sort an ObservbleCollection using linq in a getter?

JKnottJKnott Member ✭✭✭
edited January 12 in Xamarin.Forms

Hello, I have a list in a custom class, I would like to automatically return a sorted version of the list.
The best way I could accomplish this that I can think of is like this.
The alternative way would be to overload the SetProperty call to perform the sort and reassignment internally, but am not sure if that would cause cyclic loop condition.

public class ItemListDataType : EventParent //Overloads the property event handlers
{
    //These all have property fields but are excluded for space here
    public string szName;
    public string szModel;
    public string szType;
}

public class ItemListDataType : EventParent //Overloads the property event handlers
{
    //these have properties but are excluded for saving space
    private MyItemClass item; 
    private string myItemTitle;

    private ObservableCollection<ItemListDataType>  itemList

    public ObservableCollection<ItemListDataType> ItemList
        {
        //This line is throwing a 'specified cast not valid'
            get => (ObservableCollection<ItemListDataType >)itemList.OrderBy(x => x.item.szType).OrderBy(x => x.item.szModel);

            set
            {
                SetProperty(ref itemList, value, nameof(ItemList));
                OnPropertyChanged(nameof(ItemList));
            }
        }

    public ItemListDataType (MyItemClass t1, string t2)
    {
        this.item.Add(t1);
        this.myItemTitle = t2;
    }

    public ItemListDataType (MyItemClass t1)
    {
        this.item.Add(t1);
        this.myItemTitle = string.Format($"{t1.szType} {t1.szName} {t1.szModel}");
    }
}

I am using this class for populating a combobox with custom labels to custom my data type.
This code gets used like this

//I do this in this manner to cause the call to wait for the refresh to finish to ensure that the list is fully populated
//I am considering consolidation the assignment into the function, but am worried it will return before the list is populated
viewModel.ItemsInventoryList= await viewModel.RefreshItemDataList();

var list = new ItemListDataType (viewModel.ItemsInventoryList);

    cbInventory.DataSource = list.ItemsList;
    cbInventory.DisplayMemberPath = "MyItemTitle";

the prototype for the ItemsInventoryList is this

public async Task<ObservableCollection<MyItemClass >> RefreshItemDataList()
    {
        return await itemRepo.GetItems();
    }

So I guess this technically is two questions in one.
I'm sure that there is some simple cast that I am overlooking...

Best Answer

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    Accepted Answer

    Can I sort an ObservbleCollection using linq in a getter

    To sort an ObservbleCollection<T> data, try using the following code which works fine in my test.

    public class CustomViewModel
    {
        private ObservableCollection<CustomModel> dataCollection;
        public ObservableCollection<CustomModel> DataCollection
        {
            get
            {
                return  new ObservableCollection<CustomModel>(dataCollection.OrderBy(x => x.Content));
            }
            set
            {
                if (dataCollection != value)
                {
                    dataCollection = value;
                }
            }
        }
    }
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Answers

  • JKnottJKnott Member ✭✭✭
    edited January 12

    Well, I've come up with a temporary, or at least acceptable solution.

    I've created a function Sort() that handles recreating the list with a sorted version

    public void Sort()
    {
        ItemsList = new ObservableCollection<ItemListDataType>(itemList.OrderBy(x => x.item.szType).OrderBy(x => x.item.szModel));
    }
    

    Then implement it like this

    var list = new ItemListDataType (viewModel.ItemsInventoryList);
    list.Sort();
    cbInventory.DataSource = list.ItemsList;
    cbInventory.DisplayMemberPath = "MyItem
    

    If there's a better way to do it, I;d love to hear it!
    Cheers!

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    Accepted Answer

    Can I sort an ObservbleCollection using linq in a getter

    To sort an ObservbleCollection<T> data, try using the following code which works fine in my test.

    public class CustomViewModel
    {
        private ObservableCollection<CustomModel> dataCollection;
        public ObservableCollection<CustomModel> DataCollection
        {
            get
            {
                return  new ObservableCollection<CustomModel>(dataCollection.OrderBy(x => x.Content));
            }
            set
            {
                if (dataCollection != value)
                {
                    dataCollection = value;
                }
            }
        }
    }
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • JKnottJKnott Member ✭✭✭

    For some reason that's not working for me. I don't get a list returned. I'll take a deeper look into it and let you know if I have any further issues. I think it could be in the way that I am consuming the list later.

    Thanks!

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    The code works fine on my side, if it doesn't work, please post more details about the code or share a basic demo to reproduce the issue.

  • JKnottJKnott Member ✭✭✭

    It worked out, I was trying to overload the assigner with some other code, and it was problematic.
    I've got it working now, and it works great!
    Thanks much!

Sign In or Register to comment.