我目前正在尝试获取一组客户的订单列表,并找到每个客户的最后订单,检查该订单上的项目并将其与NHibernate中的字符串列表进行比较..但是我是完全难以理解我如何能够吸引客户并找到他们的最后订单日期,因为我正在使用NHibernate 1.2(我无法升级它。)所以我无法访问新版本的LINQ功能。
如果有人能指出我的方向很棒,那么到目前为止,我有这个。
IList<Order> people = new List<Order>();
ICriteria criteria = CoreHttpModule.Session.CreateCriteria(typeof(Order));
string[] instrumentList = { "Item1", "Item2", "Item3" };
public void CleanList()
{
criteria.CreateAlias("Customer", "customer", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.CreateAlias("Purchaser", "purchaser", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.CreateAlias("Agent", "agent", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
}
答案 0 :(得分:0)
首先在SQL中构建查询:
SELECT O.*
FROM Orders order1
INNER JOIN Customers customer1 ON customer1.ID = order1.CustomerId
WHERE EXISTS (
SELECT TOP 1 ID FROM Orders order2
INNER JOIN Customers customer2 ON customer2.Id = order2.CustomerId
WHERE customer2.Id = customer1.Id
ORDER BY order2.OrderDate DESC
)
然后尝试在NHibernate中构建相同的查询
DetachedCriteria dc = DetachedCriteria.For(typeof(Order), "order2")
.CreateAlias("Customer", "customer2", JoinType.InnerJoin)
.Add(Restrictions.EqProperty("customer2.ID", "customer1.ID"))
.AddOrder(NHibernate.Criterion.Order.Desc("order2.OrderDate"))
.SetProjection(Projections.Property("order2.ID"))
.SetMaxResults(1);
ICriterion condition = Subqueries.Exists(dc);
ICriteria criteria = Session.CreateCriteria(typeof(Order), "order1")
.CreateAlias("Customer", "customer1", JoinType.InnerJoin)
.Add(condition);
NHibernate唯一的限制是你不能动态创建JOIN:你只能在表(和字段)上加入JOIN在映射文件中。
请注意,这是首先使用Order
构建的,因此Customer
s没有Order
s将不会“显示”。
您必须更改字段名称。