NHibernate aggregate query for one-to-many relation

前端 未结 1 838
太阳男子
太阳男子 2021-01-22 11:30

I have next entities:

class Topic
{
    public virtual int Id {get; private set;} 
    public virtual ICollection Votes {get; private set; }
}

class         


        
相关标签:
1条回答
  • 2021-01-22 12:27

    Provided you have a summary class to store your result :

    public class  SummaryDTO
    {
        public int TopicId { get; set; }
        public VoteType TypeOfVote { get; set; }
        public int VoteCount { get; set; }
    }
    

    then

    Vote voteAlias = null;
    SummaryDTO result = null;
    
    youNHSession.QueryOver<Topic>()
      .JoinAlias(x=> x.Votes, ()=>voteAlias)
      .SelectList(
        list=>list
          .SelectGroup(topic=>topic.Id).WithAlias(()=>result.TopicId)            
          .SelectGroup(()=>voteAlias.VotedTo).WithAlias(()=>result.TypeOfVote)
          .SelectCount(()=>voteAlias.VotedTo).WithAlias(()=>result.VoteCount ))
      .TransformUsing(Transformers.AliasToBean<SummaryDTO>())
      .List<SummaryDTO>();
    

    I guess that's not exactly what you are looking for, but hope this will set you on a good track.

    0 讨论(0)
提交回复
热议问题