Language: C#
Conditional NHibernate Projection
// pseudo: if month(entered) greater than or equal to 7, return year(entered) // else return year(entered)-1 (for school year conversion) // returns distinct list of "school years" var criteria = Session.CreateCriteria<MyEntity>(); var convertToSchoolYearProjection = Projections.Conditional( Restrictions.Ge( Projections.SqlFunction("month", NHibernateUtil.Int32, Projections.Property("Entered")), 7), Projections.SqlProjection("year(EnteredDate) as schoolyear", new[] { "schoolyear" }, new IType[] { NHibernateUtil.Int32 }), Projections.SqlProjection("year(EnteredDate)-1 as schoolyear", new[] { "schoolyear" }, new IType[] { NHibernateUtil.Int32 }) ); criteria.SetProjection(Projections.Distinct(convertToSchoolYearProjection)); return InTransaction(() => criteria.List<int>() as List<int>);
Tags:
Description:
Result SQL:
SELECT distinct (case when datepart(year, this_.Entered) >= @p0 then year(Entered) else year(Entered)-1 end) as y0_ FROM dbo.MyEntities this_;@p0 = 7
SELECT distinct (case when datepart(year, this_.Entered) >= @p0 then year(Entered) else year(Entered)-1 end) as y0_ FROM dbo.MyEntities this_;@p0 = 7
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

