在我的代码中,有一个类:Person
.
当我打电话时:
var persons = session.GetObjects(session.GetClassInfo(typeof(Person)), criteria, null, 0, false, true);
persons
变量获取 ICollection
对象。
问题1:为什么不能使用persons
如IEnumerable<T>
虽然ICollection
延伸IEnumerable
?
问题2:如何构建List
实例使用 persons
?我用谷歌搜索了它,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方法。根据文档,此方法支持内部基础结构,不适合直接从您的代码中使用。
要从 session 中获取对象,我建议您使用 XPCollection 或 XPQuery:
XPCollection<Person> persons = new XPCollection<Person>(session1, criteria);
关于c# - 从 Session.GetObjects 获取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50231546/