Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

c# - Is there a way to convert an observable collection to regular collection?

I've got a test collection setup as :

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);
    }

Once I have this collection built I would like to be able to convert the Observable Collection to the type List.

The reason behind this is my main project is receiving a generic list with data I have to convert it to an Observable collection for use in gridview, listboxes etc. Data is selected within the UI and then sent back to the originating assembly for further usage.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

Cheers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...