NHibernate. Join unrelated entities

后端 未结 3 2073
自闭症患者
自闭症患者 2021-01-22 16:34

Is there any way to execute following query with native NHibernate linq provider?

var result =
    (from e1 in Session.Query()
     join e2 in Se         


        
3条回答
  •  悲&欢浪女
    2021-01-22 17:24

    I'm not quite sure when this was introduced but this is now supported by NHibernate. I am using version 3.3.1 and I have a query very similar to yours working well. The test below works for me:

    [TestFixture]
    public class NHibernateJoinUnrelatedEntitiesTest
    {
        private ISessionFactory sessionFactory;
    
        [Test]
        public void I_Can_Join_Unrelated_Entities()
        {
            // Arrange
            ISession session = sessionFactory.OpenSession();
    
            // Act
            var results = (
                              from c in session.Query()
                              from wal in session.Query()
                              where c.Id == wal.CustomerId
                                    && c.Id == 54856
                              select new { CustomerId = c.Id, Name = c.FirstName, Address = wal.IpAddress }
                          ).ToList();
    
            // Assert
            Assert.NotNull( results );
        }
    
        public class Customer
        {
            public virtual int Id { get; set; }
            public virtual string FirstName { get; set; }
        }
    
        public class WebsiteActivityLog
        {
            public virtual int Id { get; set; }
            public virtual int CustomerId { get; set; }
            public virtual string IpAddress { get; set; }
        }
    
        public class CustomerMap : ClassMap
        {
            public CustomerMap()
            {
                Id( x => x.Id );
                Map( x => x.FirstName );
            }
        }
    
        public class WebsiteActivityLogMap : ClassMap
        {
            public WebsiteActivityLogMap()
            {
                Id( x => x.Id );
                Map( x => x.CustomerId );
                Map( x => x.IpAddress );
            }
        }
    }
    

提交回复
热议问题