从 Session.GetObjects 获取列表
Getting List from Session.GetObjects
在我的代码中,有一个 class: Person
.
当我打电话时:
var persons = session.GetObjects(session.GetClassInfo(typeof(Person)), criteria, null, 0, false, true);
persons
变量获取 ICollection
个对象。
问题 1:尽管 ICollection
扩展了 IEnumerable
为什么不能将 persons
用作 IEnumerable<T>
?
问题2:如何使用persons
构建List
实例?我用谷歌搜索了一下 ToList()
方法不适用于 persons
即使 using System.Linq;
存在于代码中。
更新
代码行:
IEnumerable<Person> persons = (IEnumerable<Person>)session.GetObjects(session.GetClassInfo(typeof(Person)), criteria, null, 0, false, true);
我收到运行时错误:
Unable to cast object of type
'System.Collections.Generic.List1[System.Object]' to type
'System.Collections.Generic.IEnumerable
1[TestProject.BusinessObjects.Person]'.
我看到您正在使用 Session.GetObjects 方法。根据文档,此方法支持内部基础结构,不能直接从您的代码中使用。
要从会话中获取对象,我建议您使用 XPCollection 或 XPQuery:
XPCollection<Person> persons = new XPCollection<Person>(session1, criteria);
在我的代码中,有一个 class: Person
.
当我打电话时:
var persons = session.GetObjects(session.GetClassInfo(typeof(Person)), criteria, null, 0, false, true);
persons
变量获取 ICollection
个对象。
问题 1:尽管 ICollection
扩展了 IEnumerable
为什么不能将 persons
用作 IEnumerable<T>
?
问题2:如何使用persons
构建List
实例?我用谷歌搜索了一下 ToList()
方法不适用于 persons
即使 using System.Linq;
存在于代码中。
更新
代码行:
IEnumerable<Person> persons = (IEnumerable<Person>)session.GetObjects(session.GetClassInfo(typeof(Person)), criteria, null, 0, false, true);
我收到运行时错误:
Unable to cast object of type 'System.Collections.Generic.List
1[System.Object]' to type 'System.Collections.Generic.IEnumerable
1[TestProject.BusinessObjects.Person]'.
我看到您正在使用 Session.GetObjects 方法。根据文档,此方法支持内部基础结构,不能直接从您的代码中使用。
要从会话中获取对象,我建议您使用 XPCollection 或 XPQuery:
XPCollection<Person> persons = new XPCollection<Person>(session1, criteria);