public static IList<Program> RetrieveWorkingDays(DateTime startTime, DateTime endTime, int channelId, int maxDays) { SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program)); // where foreigntable.foreignkey = ourprimarykey sb.AddConstraint(Operator.GreaterThanOrEquals, "endTime", DateTime.Now /*startTime*/); if (maxDays > 0) { sb.AddConstraint(Operator.LessThan, "startTime", DateTime.Now.AddDays(maxDays)); } sb.AddConstraint(Operator.Equals, "idChannel", channelId); sb.AddParameter("pStartTime", typeof (DateTime)); sb.AddParameter("pEndTime", typeof (DateTime)); AddTimeRangeConstraint(sb, "startTime", "endTime", "pStartTime", "pEndTime", (endTime.Day != startTime.Day)); AddWorkingDaysConstraint(sb, "startTime"); // passing true indicates that we'd like a list of elements, i.e. that no primary key // constraints from the type being retrieved should be added to the statement SqlStatement stmt = sb.GetStatement(true); stmt.SetParameter("pStartTime", startTime); stmt.SetParameter("pEndTime", endTime); // execute the statement/query and create a collection of User instances from the result set return ObjectFactory.GetCollection<Program>(stmt.Execute()); }