c# - 从 Session.GetObjects 获取列表
问题描述
在我的代码中,有一个类:Person
.
当我打电话时:
var persons = session.GetObjects(session.GetClassInfo(typeof(Person)), criteria, null, 0, false, true);
persons
变量获取ICollection
对象。
问题1:为什么不能使用persons
asIEnumerable<T>
虽然ICollection
extends 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);
我收到运行时错误:
无法转换类型为“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);
推荐阅读
- laravel - Dusk 不执行按钮单击 & contenteditable 字段偶尔更新
- c++ - gcc 需要大量内存来编译堆栈上具有非常大对象的 c++ 文件
- asp.net-core - Razor 类库和 html 助手问题
- java - 我应该改变模式来验证什么?
- c++ - 如何修复 C++ 应用程序中缺少的 Mongo CXX 标头?(CMake)
- routes - 在 TYPO3 RouteEnhancer 中本地化 routePath 的静态段
- javascript - 我正在尝试使用 json 文件,但它不能返回数据
- vega-lite - 限制条形图以按条形宽度显示前 K 个条形
- python - 如何在 python 中的 sklearn 中的不同管道中获取特征名称
- opencv - Imagemagick 的 `+distort DePolar` 的 OpenCV 等价物是什么?