NHibernate查询中的DTO的SubQuery填充属性 [英] Fill property of DTO with SubQuery in NHibernate Query

查看:136
本文介绍了NHibernate查询中的DTO的SubQuery填充属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的DTO对象:

I have a DTO object like this:

public class TreeViewDTO
{
   public string Value { get; set; }
   public string Text { get; set; }
   public bool HasChildren { get; set; }
}

我与Nhibernate映射的实体是:

and my entity mapped with Nhibernate is:

public class Entity
{
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
   public virtual Entity Parent { get; set; }
   /* other properties */
}

我想知道,如何获取我的DTO列表并使用count方法或子查询填充HasChildren属性,以了解是否有孩子?

I would like to know, how can I get a List of my DTOs and fill the HasChildren property using a count method or a subquery to know if there are childrens?

我已经尝试过了,但是不起作用:

I have tried this, but does not work:

return Session.QueryOver<Entity>
                        .Select(entity => new TreeViewViewModel() {
                                                        Value = entity.Id.ToString(),
                                                        Text = entity.Name,
                                                        HasChildren = (Session.QueryOver<Entity>().Where(x => x.ParentId == entity.Id).RowCount() > 0)})
                        .ToList();

我有一个例外:NotSupportedException,消息显示:x => (x.Parent.Id == [100001].Id),它不受支持.

I got an exception with this: NotSupportedException and the messages says: x => (x.Parent.Id == [100001].Id) and it is not supported.

如何创建查询以填充此属性?

How could I create a query to fill this property?

PS:我想查询只选择Id,Name和Count ...,因为我的实体可以有30个或更多字段...

PS: I would like to have a query to select only the Id, Name and Count... because my entity can have 30 fields or more...

谢谢.

推荐答案

使用NHibernate Linq提供程序,则可以执行以下操作:-

Using the NHibernate Linq provider then you can do this:-

public class dto
{
    public long Value { get; set; }
    public int Count { get; set; }
    public bool HasCount { get { return Count > 0; } }
}

注意:我的DTO具有只读属性,该属性可以查看实际计数,然后查询为:-

Note: my DTO has a read-only property that looks at the actual count, the query is then:-

var a = Db.Session.Query<Support>().Select(
         s => new dto {
                        Value = s.Id,
                        Count = s.CommentList.Count
                      }
            ).ToList();

这会生成以下sQL

select support0_.Id                                   as col_0_0_,
       (select cast(count(*) as SIGNED)
        from   supportcomment commentlis1_
        where  support0_.Id = commentlis1_.SupportId) as col_1_0_
from   support support0_

我从未见过使用QueryOver的实际示例.我曾被它刺伤,但无法正常工作.

I have never seen a working example of this using QueryOver. I have had had a stab at it but couldn't get it working..

这篇关于NHibernate查询中的DTO的SubQuery填充属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆