嗨,我是NHibernate的新手,无法在聚合的自我组合中加入。我认为展示我想要实现的SQL可能有所帮助:
数据看起来像这样:
Message TABLE
ID DIRECTION BATCHID SEQUENCE ISLASTINSEQUENCE
1 Outbound 1 1 0
2 Outbound 1 2 0
3 Outbound 1 3 1
查询如下所示:
--Get all msgs with batchId where full sequence is ready
SELECT *
FROM [Message] M
JOIN (
--Group Msg on batchId and Count Sequence + cross ref using having
SELECT M.BatchId
FROM [Message] M
JOIN (
--Get BatchId of last msg in sequence
SELECT BatchId, Sequence as LastInSequence
FROM [Message]
WHERE Direction = 'Outbound'
AND IsEndOfSequence = 1
) M1 ON M.BatchId = M1.BatchId
WHERE Direction = 'Outbound'
GROUP BY M.BatchId, LastInSequence
HAVING count(1) = M1.LastInSequence
) B ON M.BatchId = B.BatchId
基本上我想要包含我有完整序列的批次
这可能是HNibernate Linq的尝试:
var lastInSeqenceMsgs =
from b in Query<Message>(x => x.Direction == MessageDirection.Outbound
&& x.IsEndOfSequence)
select new {b.BatchId, LastInSequence = b.Sequence};
var fullSequenceBatchIds =
from outboundMessage in Query<Message>(x => x.Direction ==
MessageDirection.Outbound)
join lastInSequence in (lastInSeqenceMsgs)
on outboundMessage.BatchId equals lastInSequence.BatchId
group lastInSequence by new {lastInSequence.BatchId, lastInSequence.LastInSequence}
into g
where g.Count() == g.Key.LastInSequence
select g.Key.BatchId;
var allMsgsFromWithCompleteSequences =
from fullSequenceMessage in Query<Message>(x => x.Direction ==
MessageDirection.Outbound)
join test in (fullSequenceBatchIds) on
fullSequenceMessage.BatchId equals test.BatchId
select test;
它对第二个查询进行了轰炸(我评估了查询 - 未显示),但有以下异常:
无法转换类型为#System; Linq.Expressions.NewExpression&#39;的对象输入&#39; Remotion.Linq.Clauses.Expressions.QuerySourceReferenceExpression&#39;。
其中我提炼回到自我加入
var fullSequenceBatchIds =
from outboundMessage in Query<Message>(x => x.Direction ==
MessageDirection.Outbound)
join lastInSequence in (lastInSeqenceMsgs)
on outboundMessage.BatchId equals lastInSequence.BatchId
select outboundMessage;
我得到了例外 &#34;不支持指定的方法。&#34;
我的额头上有砖印,所以非常感谢这里的一些帮助。
答案 0 :(得分:0)
一般来说,我只能说nhibernate Linq提供商不支持Linq提供的所有内容。
此外,nhibernate不支持子查询。它根本不起作用,你必须以某种方式解决它。