有没有办法将可观察集合转换为常规集合?

发布于 2024-09-08 00:31:23 字数 741 浏览 3 评论 0原文

我有一个测试集合设置为:

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

一旦构建了这个集合,我希望能够将可观察集合转换为列表类型。

这背后的原因是我的主要项目正在接收一个包含数据的通用列表,我必须将其转换为可观察集合以在网格视图、列表框等中使用。数据在 UI 中选择,然后发送回原始程序集以供进一步使用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小ぇ时光︴ 2024-09-15 00:31:23

我认为最快的方法是使用 LINQ。

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

干杯。

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

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

Cheers.

好菇凉咱不稀罕他 2024-09-15 00:31:23

请尝试以下操作

var list = MyselectedPeople.ToList();

确保您将 System.Linq 作为您的 using 语句之一。

Try the following

var list = MyselectedPeople.ToList();

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

2024-09-15 00:31:23

这应该可以做到...

List<Person> myList = MyselectedPeople.ToList<Person>();

This should do it...

List<Person> myList = MyselectedPeople.ToList<Person>();
习ぎ惯性依靠 2024-09-15 00:31:23

我只是想指出,除了明显的 Linq 扩展方法之外,List 始终有一个需要 IEnumerable 的重载

return new List<Person>(MyselectedPeople);

I just want to point out that aside from the obvious Linq extension method, List has always had an overload that takes an IEnumerable<T>

return new List<Person>(MyselectedPeople);
愛放△進行李 2024-09-15 00:31:23

奇怪的是,您的后端程序集被编码为仅接受 List。这是非常严格的,并且阻止您执行有用的操作,例如传递数组、ObservableCollectionCollectionReadOnlyCollection;T>,或者Dictionary的Keys或Values属性,或者任何其他类似列表的东西。

如果可能,请更改后端程序集以接受 IList。然后,您可以按原样传入 ObservableCollection,而无需将其内容复制到 List 中。

It's odd that your back-end assembly is coded to only accept List<T>. That's very restrictive, and prevents you from doing useful things like passing an array, or an ObservableCollection<T>, or a Collection<T>, or a ReadOnlyCollection<T>, or the Keys or Values properties of a Dictionary<TKey, TValue>, or any of the myriad of other list-like things out there.

If possible, change your back-end assembly to accept an IList<T>. Then you can just pass in your ObservableCollection<T> as-is, without ever needing to copy its contents into a List<T>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文