I have this nhibernate query:
var q =
NHibernateSession.Curr开发者_StackOverflow社区ent.CreateSQLQuery
(
@"SELECT LastestEvents.*
FROM (
SELECT DISTINCT SbQcontainer.Container
FROM HistoricEvents
SbQcontainer WHERE SbQcontainer.LineCompany_Id = :lineCompany) as Sbq
JOIN HistoricEvents as LastestEvents
ON LastestEvents.id = (
SELECT TOP(1) id
FROM HistoricEvents mi
WHERE mi.Container = Sbq.Container and mi.LineCompany_Id = :lineCompany
ORDER BY mi.Date DESC
)"
).SetResultTransformer(Transformers.AliasToBean(typeof(HistoricEvent)));
q.SetParameter("lineCompany",lineCompany.Id);
q.SetCacheable(false);
var results = q.List<HistoricEvent>().ToList();
It looks for the lastest events on each container for the given lineCompany, it works, but i dont know how to set this resultset to a list of T HistoricEvent, i try this line:
.SetResultTransformer(Transformers.AliasToBean(typeof(HistoricEvent)));
But throws NHibernate.PropertyNotFoundException:Could not find a setter for property 'Event_Id' in class 'HistoricEvent'.
Is there any way to do this?, or maybe doing this same query using the ICriteria API?
Thx in advance.
Assuming HistoricEvent is a mapped entity, the following should give you what you're looking for:
var q =
NHibernateSession.Current.CreateSQLQuery
(
@"SELECT LastestEvents.*
FROM (
SELECT DISTINCT SbQcontainer.Container
FROM HistoricEvents
SbQcontainer WHERE SbQcontainer.LineCompany_Id = :lineCompany) as Sbq
JOIN HistoricEvents as LastestEvents
ON LastestEvents.id = (
SELECT TOP(1) id
FROM HistoricEvents mi
WHERE mi.Container = Sbq.Container and mi.LineCompany_Id = :lineCompany
ORDER BY mi.Date DESC
)"
).AddEntity(typeof(HistoricEvent));
q.SetParameter("lineCompany",lineCompany.Id);
q.SetCacheable(false);
var results = q.List<HistoricEvent>().ToList();
See the relevant documentation for further details.
精彩评论