NHibernate.加入无关实体 [英] NHibernate. Join unrelated entities
问题描述
是否可以通过本机NHibernate linq提供程序执行以下查询?
Is there any way to execute following query with native NHibernate linq provider?
var result =
(from e1 in Session.Query<Entity1>()
join e2 in Session.Query<Entity2>() on e1.SomeField equals e2.SomeField into je
from je2 in je.DefaultIfEmpty()
select new {e1.Id, e1.SomeField, je2.SomeUrelatedField}).ToList()
当前我正在将Fluent NHibernate 1.2与NHibernate 3.1结合使用,并且得到NotImplementedException
Currently I'm using Fluent NHibernate 1.2 with NHibernate 3.1 and I get NotImplementedException
我不想介绍e1
和e2
之间的任何关系.通过设计,它们没有任何关系,并且像上面这样的查询可以在极少数情况下使用.
I don't want to introduce any relation between e1
and e2
. By design they have no a relation and query like above could be used in rare purposes.
此博客文章表示不支持.现在呢?
This blog post tells that it was unsupported. What about now?
推荐答案
我不太确定何时引入此方法,但是NHibernate现在支持此方法.我正在使用3.3.1版,并且有一个查询与您的查询非常相似.以下测试对我有效:
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<Customer>()
from wal in session.Query<WebsiteActivityLog>()
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<Customer>
{
public CustomerMap()
{
Id( x => x.Id );
Map( x => x.FirstName );
}
}
public class WebsiteActivityLogMap : ClassMap<WebsiteActivityLog>
{
public WebsiteActivityLogMap()
{
Id( x => x.Id );
Map( x => x.CustomerId );
Map( x => x.IpAddress );
}
}
}
这篇关于NHibernate.加入无关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!