Is there a way to convert an observable collection into a regular collection?

I have a test collection like:

ObservableCollection<Person> MyselectedPeople = new ObservableCollection<Person>(); public MainWindow() { InitializeComponent(); FillData(); } public void FillData() { Person p1 = new Person(); p1.NameFirst = "John"; p1.NameLast = "Doe"; p1.Address = "123 Main Street"; p1.City = "Wilmington"; p1.DOBTimeStamp = DateTime.Parse("04/12/1968").Date; p1.EyeColor = "Blue"; p1.Height = "601"; p1.HairColor = "BRN"; MyselectedPeople.Add(p1); } 

As soon as I build this collection, I would like to be able to convert the Observable Collection into a list of types.

The reason for this is my main project - getting a shared list with data that I have to convert to an Observable collection for use in gridview, listboxes, etc. Data is selected in the user interface, and then sent back to the original assembly for future use.

+6
c # wpf observablecollection
source share
5 answers

I think the fastest way to do this is with LINQ.

  List<Person> personList= MySelectedPeople.ToList(); 

Greetings.

+10
source share

Try to execute

 var list = MyselectedPeople.ToList(); 

Make sure you have System.Linq as one of your statements.

+8
source share

That should do it ...

 List<Person> myList = MyselectedPeople.ToList<Person>(); 
+5
source share

I just want to point out that besides the obvious extension method, Linq List always had an overload that accepts IEnumerable<T>

 return new List<Person>(MyselectedPeople); 
+4
source share

It is odd that your back assembly is encoded to accept only List<T> . This is very restrictive and prevents you from doing useful things like passing an array, or ObservableCollection<T> or Collection<T> , or ReadOnlyCollection<T> , or the Keys or Values Dictionary<TKey, TValue> , or any of myriad other things like lists.

If possible, modify your back assembly to accept IList<T> . Then you can simply pass your ObservableCollection<T> as it is, without having to copy its contents to List<T> .

+2
source share

All Articles