Jaycee Evangelista
Jaycee Evangelista

Reputation: 1127

How to convert ToList to ObservableCollection?

Good Day Everyone. I'm creating a Xamarin.Forms Portable Application I just want to ask how am I going to convert this expression from List to ObservableCollection. Take a look at the 'ToList();' code. I don't know how to change it in order for it to read an observable collection.

 CustomerList = _searchedCustomerList.Where(r => r.CUSTOMER_NAME.ToLower().Contains(_keyword.ToLower())).ToList();

I'm having problem how to do this because I prefer to use an ObservableCollection rather than the List. So I declare the CustomerList as ObservableCollection.

    public ObservableCollection<Customer> CustomerList
    {
        get
        {
            return _customerList;
        }
        set
        {
            _customerList = value;
            OnPropertyChanged();
        }
    }

Is there anyway to do this? Thanks a lot.

Upvotes: 2

Views: 2153

Answers (2)

Raskayu
Raskayu

Reputation: 743

ObservableCollection <T> have constructor which takes IEnumerable <T>

Example:

ObservableCollection<Customer> myCollection = new ObservableCollection<Customer>(myList);

Upvotes: 9

Oscar
Oscar

Reputation: 13960

You can use the constructor:

public ObservableCollection( 
    List<T> list 
)

https://msdn.microsoft.com/en-us/en-en/library/ms653202(v=vs.110).aspx

Just create a new ObservableCollection and pass the list as an argument to the constructor.

Upvotes: 3

Related Questions