我想要實現:NHibernate Linq .Contains代碼生成有bug?
select StoreId, StoreName from Store where StoreId in (
select StoreId from Employee where EmployeeName = 'Steve Jobs')
我有這樣的代碼:
public class Store
{
public virtual int StoreId { get; private set; }
public virtual string StoreName { get; set; }
public virtual IList<Employee> Staff { get; set; }
}
public class Employee
{
public virtual Store Store { get; set; }
public virtual int EmployeeId { get; private set; }
public virtual string EmployeeName { get; set; }
}
var q = from s in session.Query<Store>()
where
(from e in session.Query<Employee>()
where s.EmployeeName == "Steve Jobs"
select e.Store.StoreId).Contains(s.StoreId)
select s;
NHibernate的生成這個(別名剝離):
select s.StoreId, s.StoreName
from Store s
where exists
(
select t.StoreId
from Employee e
left join Store t on t.StoreId = e.StoreId
where e.EmployeeName = 'Steve Jobs'
-- wrongly generated code
and t.EmployeeId = s.StoreId
)
的LINQ toSql正確生成的代碼:
select s.StoreId, s.StoreName
from Store s
where exists
(
select null
from Employee e
where e.EmployeeName = 'Steve Jobs'
and e.StoreId = s.StoreId
)
Linq上的NHibernate的子查詢代碼生成有問題嗎?
然而,HQL作品:
var q = session.CreateQuery("from Store as s where s.StoreId in (select e.WorkingInStore.StoreId from Employee as e where e.EmployeeName = 'lennon')").List<Store>();
酷,我應該想到這一點,我用** **任何SQL中之前,也許我試圖太多非常的Linq-Y辦法第一和靈感這個http://blog.wekeroad.com/2008/02/27/creating-in-queries-with-linq-to-sql,儘可能避免使用lambda和擴展方法。但在這種情況下,任何**看起來比純粹的Linq方法更優雅,並不是所有的SQL概念都可以在Linq語法中捕獲,擴展方法是有保證的**。Distinct()**想到了,http: //www.ienablemuch.com/2010/12/performing-order-by-on-distinct-on-linq.html – 2010-12-08 23:30:40