我知道有很多类似的话题,但我找不到我要搜索的答案。
我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
从查询中获取消息和接收者,但这两种方法都不起作用。
答案 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"];