简体   繁体   English

从实体集合返回可观察的集合

[英]Return an observable collection from an entity collection

I have the following collection and I would like to return 我有以下收藏,我想退货

ObservableCollection<Person> people = new ObservableCollection<Person>();
foreach (var p in context.Persons.ToList())
{
    people.add((Person)p);
}

How can I do it without having to loop through the collection? 我如何做而不必遍历整个收藏集?

Use ObservableCollection constructor that has list as a parameter: 使用以list作为参数的ObservableCollection构造函数:

ObservableCollection<Person> people = new ObservableCollection<Person>(context.Persons.ToList());

From the docs : 文档

Initializes a new instance of the ObservableCollection class that contains elements copied from the specified list. 初始化ObservableCollection类的新实例,该实例包含从指定列表复制的元素。

用列表初始化ObservableCollection

people = new ObservableCollection<Person>(context.Persons);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM