I have an entity:
class Entity
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
}
I want to select sum of (A-B-C). So I want to run sql like this:
SELECT SUM(A-B-C) FROM Entity
I can achieve it by SqlProjection:
QueryOver.Of<Entity>().Select(Projections.SqlProjection("SUM(A-B-C) AS total", new[] { "total" }, new IType[] { NHibernateUtil.Int32 }));
But I do not want to use strings. How it can be done in other way?
Unfortunately NHibernate doesn't have built in arithmetic operators. Piggybacking on this question and answer, here are a few options:
Use
VarArgsSQLFunction
directly:This is the most straightforward way to accomplish this, but there are a few ways to dress it up.
Create your own dialect and register a
-
function:This basically allows you to avoid redefining the
-
function every time you use it, and is a bit cleaner.You can go even further and refactor the projection into an extension method:
All of these generate the following SQL: