We have a NHibernate V5 project were some queries need improvement. We had one query using ICriteria like this:
var list = session.CreateCriteria(typeof(ClockEntry))
.Add(Restrictions.And(
Restrictions.Ge("Time", startDate),
Restrictions.Le("Time", endDate)
))
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Id"))
.Add(Projections.Property("Time"))
)
.List<IList>()
.Select(ce => new ClockEntry() { Id = (Guid)ce[0], Time = (DateTime)ce[1] });
The last line of code (the Select) creates a new ClockEntry list where ONLY the Id and Time retain values. All other fields (columns) are null.
We then wrote a new query using NHibernate Query where the "Contains" element was added. This is as follows:
var p = Rules.UserDataFilters.UserPayrollFilterNumbers(user, session, false);
var s = Rules.UserDataFilters.UserSiteFilterCodes(user, session);
ClockEntryRepository clockEntryRepository = new ClockEntryRepository(session);
var r = clockEntryRepository
.Query()
.Where(c =>
c.Time >= startDate &&
c.Time <= endDate &&
p.Contains(c.Employee.Payroll.Number) &&
s.Contains(c.Employee.Site.Code));
Notice the added 'p' and 's' constraints
The problem: I am unable to find the correct construct to add the 'Contains' into the first code block and I am unable to find a elegant way in which to achieve the Select statement in the 2nd example. Obviously either one will do but I would greatly appreciate input for answers for both scenarios.
The Icriteria query "p" and "s" items I managed to resolve once I figured out that in SQL this would be a IN construct so it became: