在NHibernate中不使用CreateSQLQuery就可以做到这一点吗?

在NHibernate中不使用CreateSQLQuery就可以做到这一点吗?,nhibernate,Nhibernate,是否可以在NHibernate中不使用CreateSQLQuery执行此操作。最好与Linq到Nhibernate一起使用。最大的问题是如何不使用主键 SELECT DISTINCT calEvent.* From CalendarEvent as calEvent LEFT JOIN UserChannelInteraction as channelInteraction on channelInteraction.ChannelToFollow_id = calEvent.Channel_i

是否可以在NHibernate中不使用CreateSQLQuery执行此操作。最好与Linq到Nhibernate一起使用。最大的问题是如何不使用主键

SELECT DISTINCT calEvent.* From CalendarEvent as calEvent
LEFT JOIN UserChannelInteraction as channelInteraction on channelInteraction.ChannelToFollow_id = calEvent.Channel_id
LEFT JOIN UserCalendarEventInteraction as eventInteraction on eventInteraction.CalendarEvent_id = calEvent.Id
LEFT JOIN UserChannelInteraction as eventInteractionEvent on eventInteractionEvent.UserToFollow_id = eventInteraction.User_id
WHERE (calEvent.Channel_id = @intMainChannelID 
OR channelInteraction.User_id = @intUserID
OR eventInteraction.User_id = @intUserID
OR (eventInteractionEvent.User_id = @intUserID AND eventInteraction.Status = 'Accepted'))
AND calEvent.StartDateTime >= @dtStartDate 
AND calEvent.StartDateTime <= @dtEndDate
ORDER BY calEvent.StartDateTime asc
从CalendarEvent中选择DISTINCT calEvent.*作为calEvent
在channelInteraction.ChannelToFollow\u id=calEvent.Channel\u id上将UserChannelInteraction作为channelInteraction左连接
LEFT JOIN UserCalendarEventInteraction作为eventInteraction上的eventInteraction.CalendarEvent\u id=calEvent.id
在eventInteractionEvent.UserToFollow\u id=eventInteraction.User\u id上将UserChannelInteraction作为eventInteractionEvent加入
其中(calEvent.Channel_id=@intMainChannelID)
或channelInteraction.User_id=@intUserID
或eventInteraction.User_id=@intUserID
或者(eventInteractionEvent.User_id=@intUserID和eventInteraction.Status='Accepted'))
和calEvent.StartDateTime>=@dtStartDate

和calEvent.StartDateTimeHmmm。。。也许您需要尝试利用子查询


检查此项:

您可以使用进行任意连接。θ连接是笛卡尔乘积,因此它会产生所有可能的组合,然后可以对这些组合进行过滤

在NHibernate中,您可以像这样执行θ样式的联接(HQL):

然后,您可以添加您想要的任何筛选条件,对结果进行排序,以及您可能想要执行的所有其他操作

from Book b, Review r where b.Isbn = r.Isbn where b.Title = 'My Title' or r.Name = 'John Doe' order by b.Author asc
关于theta加入Hibernate(不是NHibernate,但它仍然相关)

然而,由于theta连接是笛卡尔积,在使用该方法进行三连接查询之前,您可能需要三思而后行,并进行一些性能测试

from Book b, Review r where b.Isbn = r.Isbn where b.Title = 'My Title' or r.Name = 'John Doe' order by b.Author asc