sql to nhibernate <p>I need to convert this SQL query to nhibernate</p> <pre><code>declare @i int set @i=2 while (@i&gt;0) begin insert into #tmp_table3 (id , descr , RangeType , createdate ) select * from AgeRanges as ar where AgeRangeID in ( select AgeRangeID as id from AgeRangeDetails where agerangeid in (select AgeRangeID from agerangedetails group by AgeRangeID having COUNT(AgeRangeID)=2) and agestart =(select AgeStart from #tmp_table2 where id=@i) and AgeEnd = (select AgeEnd from #tmp_table2 where id=@i) and Payment = (select Payment from #tmp_table2 where id=@i) group by AgeRangeID) and AgeRangeType=0 and AgeRangeID&lt;&gt;5 set @i=@i-1 end select id from #tmp_table3 where id in (select id from #tmp_table3 group by id having COUNT(id)=2) group by id </code></pre> <p>can someone help me to do this. thanks</p>