如何使用 nhibernate 获取 DTO 成员的集合?

发布于 2024-07-27 01:25:22 字数 531 浏览 1 评论 0原文

我需要获取填充在 DTO 中的集合属性,但我无法找到有关执行此操作的任何信息。

我尝试这样做:

ICriteria selectCriteria = Session.CreateCriteria<DataRun>()
            .SetProjection(Projections.ProjectionList()
                .Add(Projections.Property("SomeCollection"), "Collection"))
            .SetResultTransformer(Transformers.AliasToBean<MyDto>());

但 MyDto.Collection 始终为空。 我做错了吗,这可能吗?

另外,我最初计划使用子查询来完成此操作,这样我就可以用其他 DTO 填充我的 DTO 集合,但这不起作用,因为子查询的结果超过 1 行(理应如此),而 Sqlit 不喜欢这样(抛出异常)。 在这里做什么是正确的?

I need to get a collection property populated in a DTO and I'm having trouble finding any information on doing this.

I tried to do it like this:

ICriteria selectCriteria = Session.CreateCriteria<DataRun>()
            .SetProjection(Projections.ProjectionList()
                .Add(Projections.Property("SomeCollection"), "Collection"))
            .SetResultTransformer(Transformers.AliasToBean<MyDto>());

but MyDto.Collection is always null. Am I doing this wrong, is this even possible?

Also, I had originally planned on doing this with a SubQuery so I could just fill my DTO's collection with other DTOs but this will not work because the result of the subquery has more than 1 row (as it should) and Sqlit does not like that (throws an exception). what is the right thing to do here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瘫痪情歌 2024-08-03 01:25:23

我使用以下语法在项目中检索了集合属性:

public IList<ChildCollectionType> GetChildObjectsByParentId(Int32 id)
{
    ISession session = GetSession();//Get NHibernate session routine
    return session.Load<ParentDTO>(id).ChildCollectionProperty;
}

其中 id 是父对象的键。

I've retrieved collection properties in my project with the following syntax:

public IList<ChildCollectionType> GetChildObjectsByParentId(Int32 id)
{
    ISession session = GetSession();//Get NHibernate session routine
    return session.Load<ParentDTO>(id).ChildCollectionProperty;
}

where id is the key of the parent object.

丶情人眼里出诗心の 2024-08-03 01:25:23

您可以使用自定义转换。

ICriteria selectCriteria = Session.CreateCriteria<DataRun>()
            .SetProjection(Projections.ProjectionList()
                .Add(Projections.Property("SomeCollection"), "Collection"))
            .TransformUsing(new CustomTransformer());

这是您的自定义变压器实现

public class CustomTransformer : IResultTransformer
    {
        public System.Collections.IList TransformList(System.Collections.IList collection)
        {
            return collection;
        }

        public object TransformTuple(object[] tuple, string[] aliases)
        {
            return new MyDto
            {
                //map your data to dto and convert to data type if needed
                YourProperty1 = tuple[0],
                YourProperty12 = (int)tuple[1]
            };
        }
    }

You may use custom transformation.

ICriteria selectCriteria = Session.CreateCriteria<DataRun>()
            .SetProjection(Projections.ProjectionList()
                .Add(Projections.Property("SomeCollection"), "Collection"))
            .TransformUsing(new CustomTransformer());

Here is your custom transformer implementation

public class CustomTransformer : IResultTransformer
    {
        public System.Collections.IList TransformList(System.Collections.IList collection)
        {
            return collection;
        }

        public object TransformTuple(object[] tuple, string[] aliases)
        {
            return new MyDto
            {
                //map your data to dto and convert to data type if needed
                YourProperty1 = tuple[0],
                YourProperty12 = (int)tuple[1]
            };
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文