NHibernate JoinQueryOver返回所有子类型条目

时间:2016-03-29 13:45:43

标签: nhibernate queryover

我知道有很多类似的话题,但我找不到我要搜索的答案。

Message列有Receivers。我试图获取状态为new的所有消息和类型为A的接收者。我得到所有消息都有相应的接收器,但所有的接收器。我只想得到我感兴趣的接收器。

以下是我要使用的内容:

var messages = session.QueryOver<MessageDTO>()
    .Where(message => message.State == MessageState.New)
    .JoinQueryOver<MessageReceiverDTO>(message => message.MessageReceivers)
    .Where(receiver => receiver.Type == ReceiverType.A)
    .TransformUsing(Transformers.DistinctRootEntity)
    .List();
    //.Where(message => message.MessageReceivers.Count > 0)
    //.ToList();

很高兴能得到帮助。我也尝试使用Select从查询中获取消息和接收者,但这两种方法都不起作用。

2 个答案:

答案 0 :(得分:0)

尝试使用别名的QueryOver:

MessageDTO messageAlias = null;
MessageReceiverDTO receiverAlias = null;

var query =
    session.QueryOver<MessageDTO>(() => messageAlias)
         .JoinAlias(() => messageAlias.Receivers, () => receiverAlias)
         .Where(() => messageAlias.State == MessageState.New)
         .And(() => receiverAlias.Type == ReceiverType.A);

答案 1 :(得分:0)

认为 Transformers.AliasToEntityMap在这里可能有用。基本上,它会将生成的查询中的每一行转换为IDictionary,其中的键对应于别名和与实体对应的值。例如:

MessageDTO message = null;
MessageReceiverDTO receiver = null;

var messages = session.QueryOver<MessageDTO>(() => messageAlias)
    .Where(message => message.State == MessageState.New)
    .JoinQueryOver<MessageReceiverDTO>(message => message.MessageReceivers, () => receiver)
    .Where(receiver => receiver.Type == ReceiverType.A)
    .TransformUsing(Transformers.AliasToEntityMap)
    .List<IDictionary>();

MessageDTO firstMessage = (MessageDTO)messages[0]["message"];
MessageReceiverDTO firstReceiver = (MessageReceiverDTO)messages[0]["receiver"];