1 回答
TA贡献1963条经验 获得超6个赞
如果我正确地理解你,你有这样的数据,你想要标记的记录。我不知道是否是桌子,所以我不依赖它。iventoryitemonhandidid
Table InventoryItemBUOnHand (mapped as SiteItemOnHand)
| businessunitid|inventoryitemid|lastmodifiedtimestamp|inventoryitemonhandid|
| 829| 939| 2019-01-01 00:00:00| 100| <--
| 829| 940| 2019-01-02 00:00:00| 101|
| 829| 940| 2019-01-03 00:00:00| 102| <--
| 829| 950| 2019-01-04 00:00:00| 103|
| 829| 950| 2019-01-10 00:00:00| 104| <--
| 829| 950| 2019-01-06 00:00:00| 105|
如果是,那么我会使用子查询,如以下问题:SO:NHibernate - 从聚合中选择完整记录
根据您的要求进行修改(未测试),如下所示:
int siteID = 829;
List<int> itemIdList = new List<int>() { 939, 940, 950 };
SiteItemOnHand siAlias = null;
var subQuery = QueryOver.Of<SiteItemOnHand>()
.Where(x => x.Businessunitid == siAlias.Businessunitid)
.And(x => x.ItemID == siAlias.ItemID)
.Select(Projections.Max<SiteItemOnHand>(y => y.lastmodifiedtimestamp));
var siteItems = Session.QueryOver<SiteItemOnHand>(() => siAlias)
.Where(x => x.Businessunitid == siteID)
.AndRestrictionOn(x => x.ItemID).IsIn(itemIdList.ToArray())
.WithSubquery.Where(x => siAlias.lastmodifiedtimestamp == subQuery.As<DateTime>())
.List();
此处的目标是使用子查询筛选每个组的正确最大日期,然后使用它来筛选实际记录。
生成的 SQL 将如下所示:
SELECT <field_list> FROM InventoryItemBUOnHand
WHERE Businessunitid = 829
AND inventoryitemid in (939, 940, 950)
AND this_.lastmodifiedtimestamp =
(SELECT max(this_0_.lastmodifiedtimestamp) as y0_
FROM InventoryItemBUOnHand this_0_
WHERE this_0_.Businessunitid = this_.Businessunitid and this_0_.ItemID = this_.ItemID)
谨慎:当两条记录具有相同的值时,比较可能会导致不需要的结果。如果发生这种情况,您可以添加并仅选择第一条记录。如果是唯一索引,则变得更加简单。lastmodifiedtimestampbusinessunitidinventoryitemidOrderByinventoryitemonhandid
- 1 回答
- 0 关注
- 78 浏览
添加回答
举报