Skip to content

Commit 607657c

Browse files
PleasantDhazzik
authored andcommitted
NH-3094 - Support unary minus and plus operators in LINQ (#563)
1 parent 7088fe9 commit 607657c

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

src/NHibernate.Test/Linq/OperatorTests.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Linq.Expressions;
45
using System.Text;
56
using NHibernate.DomainModel.Northwind.Entities;
67
using NHibernate.Linq;
@@ -16,5 +17,21 @@ public void Mod()
1617
{
1718
Assert.AreEqual(2, session.Query<TimesheetEntry>().Where(a => a.NumberOfHours % 7 == 0).Count());
1819
}
19-
}
20+
21+
[Test]
22+
public void UnaryMinus()
23+
{
24+
Assert.AreEqual(1, session.Query<TimesheetEntry>().Count(a => -a.NumberOfHours == -7));
25+
}
26+
27+
[Test]
28+
public void UnaryPlus()
29+
{
30+
// Ensure expression tree contains UnaryPlus
31+
var param = Expression.Parameter(typeof(TimesheetEntry), "e");
32+
var expr = Expression.Equal(Expression.UnaryPlus(Expression.PropertyOrField(param, "NumberOfHours")), Expression.Constant(7));
33+
var predicate = Expression.Lambda<Func<TimesheetEntry, bool>>(expr, param);
34+
Assert.AreEqual(1, session.Query<TimesheetEntry>().Count(predicate));
35+
}
36+
}
2037
}

src/NHibernate/Hql/Ast/HqlTreeBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public HqlDivide Divide(HqlExpression lhs, HqlExpression rhs)
124124
return new HqlDivide(_factory, lhs, rhs);
125125
}
126126

127+
public HqlNegate Negate(HqlExpression expression)
128+
{
129+
return new HqlNegate(_factory, expression);
130+
}
131+
127132
public HqlDot Dot(HqlExpression lhs, HqlExpression rhs)
128133
{
129134
return new HqlDot(_factory, lhs, rhs);

src/NHibernate/Hql/Ast/HqlTreeNode.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ public HqlAdd(IASTFactory factory, HqlExpression lhs, HqlExpression rhs)
322322
}
323323
}
324324

325+
public class HqlNegate : HqlExpression
326+
{
327+
public HqlNegate(IASTFactory factory, HqlExpression expression)
328+
: base(HqlSqlWalker.UNARY_MINUS, "-", factory, expression)
329+
{
330+
}
331+
}
332+
325333
public class HqlBooleanOr : HqlBooleanExpression
326334
{
327335
public HqlBooleanOr(IASTFactory factory, HqlBooleanExpression lhs, HqlBooleanExpression rhs)

src/NHibernate/Linq/Visitors/HqlGeneratorExpressionTreeVisitor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ protected HqlTreeNode VisitUnaryExpression(UnaryExpression expression)
438438
{
439439
switch (expression.NodeType)
440440
{
441+
case ExpressionType.Negate:
442+
return _hqlTreeBuilder.Negate(VisitExpression(expression.Operand).AsExpression());
443+
case ExpressionType.UnaryPlus:
444+
return VisitExpression(expression.Operand).AsExpression();
441445
case ExpressionType.Not:
442446
return _hqlTreeBuilder.BooleanNot(VisitExpression(expression.Operand).ToBooleanExpression());
443447
case ExpressionType.Convert:

0 commit comments

Comments
 (0)