如何在LINQ for NHibernate语句或查询中转换它? 有可能吗?看起来像标准的东西(最新的相关对象必须符合一些标准)。
select item, tag
from MyItem item
join item.Tags tag
where tag.Id = (
select max(tag2.Id)
from MyItem item2
join item2.Tags tag2
where item2.Id = item.Id
group by item2.Id
)
致以最诚挚的问候,
答案 0 :(得分:1)
好的,再次回答我自己的问题。 LINQ就是这么简单。
var resi = from x in session.Query<MyItem>()
let maxTagId = x.Tags.Max(tag => tag.Id)
select new { Item = x, Tag = x.Tags.Single(tag => tag.Id == maxTagId)};
生成的sql看起来像
SELECT myitem0_.id AS
col_0_0_,
(SELECT mytag2_.id
FROM myitem_mytag tags1_,
[mytag] mytag2_
WHERE myitem0_.id = tags1_.myitemid
AND tags1_.mytagid = mytag2_.id
AND mytag2_.id = (SELECT Cast(Max(mytag4_.id) AS INT)
FROM myitem_mytag tags3_,
[mytag] mytag4_
WHERE myitem0_.id = tags3_.myitemid
AND tags3_.mytagid = mytag4_.id)) AS
col_1_0_,
myitem0_.id AS
Id0_,
myitem0_.name AS
Name0_
FROM [myitem] myitem0_
并且看起来在语义上是正确的。