C# 从列表框中检索对象列表

C# 从列表框中检索对象列表,c#,wpf,linq,C#,Wpf,Linq,我有一个包含listboxitems的列表框。 在listboxitems中,我有域对象。 如何将列表框的项转换为域对象列表? 我现在有这样的东西: List<DomainObject> list = theListBox.Items.Cast<ListViewItem>() .Select(item => item.Content) .ToList(

我有一个包含listboxitems的列表框。 在listboxitems中,我有域对象。 如何将列表框的项转换为域对象列表? 我现在有这样的东西:

List<DomainObject> list = theListBox.Items.Cast<ListViewItem>()
                             .Select(item => item.Content)
                             .ToList();
这些对象通过.content属性存储在listboxitems中。
现在我只需要列表框的listboxitems中的PersonObject的列表。

您必须将输出转换为
Select()
方法中的
DomainObject
,例如:

List<DomainObject> list = theListBox.Items.Cast<ListViewItem>()
                          .Select(item => new DomainObject() { Content = item.Content})
                          .ToList();
List List=listbox.Items.Cast()
.Select(item=>newdomainobject(){Content=item.Content})
.ToList();

我不确定
DomainObject
类型具有什么属性,但您可以在对象初始化中填充这些属性。

“这不起作用…”是您收到的错误消息吗?在listboxitems中,我有域对象。如何在listboxitems中存储域对象?错误5无法通过其内容属性将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”。编辑了我的帖子以使其更清晰。这篇文章的方向正确,但现在我得到了这个错误:错误3'System.Windows.Controls.ItemCollection'不包含'Select'的定义,并且找不到接受'System.Windows.Controls.ItemCollection'类型的第一个参数的扩展方法'Select'(您缺少using指令或程序集引用吗?)在调用
Select()
之前,先调用
.Cast
。这一个对我有用,非常感谢!
List<DomainObject> list = theListBox.Items.Cast<ListViewItem>()
                          .Select(item => new DomainObject() { Content = item.Content})
                          .ToList();