首页 > 解决方案 > 从 Session.GetObjects 获取列表

问题描述

在我的代码中,有一个类:Person.

当我打电话时:

var persons = session.GetObjects(session.GetClassInfo(typeof(Person)), criteria, null, 0, false, true);

persons变量获取ICollection对象。

问题1:为什么不能使用personsasIEnumerable<T>虽然ICollectionextends IEnumerable

问题2:如何使用构建List实例persons?我用谷歌搜索了它,甚至代码中ToList()都没有方法可用。personsusing System.Linq;

更新

在代码行:

IEnumerable<Person> persons = (IEnumerable<Person>)session.GetObjects(session.GetClassInfo(typeof(Person)), criteria, null, 0, false, true);

我收到运行时错误:

无法转换类型为“System.Collections.Generic.List 1[System.Object]' to type 'System.Collections.Generic.IEnumerable1 [TestProject.BusinessObjects.Person]”的对象。

标签: c#devexpress

解决方案


我看到您正在使用Session.GetObjects方法。根据文档,此方法支持内部基础架构,不打算直接从您的代码中使用

要从会话中获取对象,我建议您使用 XPCollection 或 XPQuery:

XPCollection<Person> persons = new XPCollection<Person>(session1, criteria);

推荐阅读