Skip to content

NH-3094: Support unary minus and plus operators in LINQ #563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/NHibernate.Test/Linq/OperatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using NHibernate.DomainModel.Northwind.Entities;
using NHibernate.Linq;
Expand All @@ -16,5 +17,21 @@ public void Mod()
{
Assert.AreEqual(2, session.Query<TimesheetEntry>().Where(a => a.NumberOfHours % 7 == 0).Count());
}
}

[Test]
public void UnaryMinus()
{
Assert.AreEqual(1, session.Query<TimesheetEntry>().Count(a => -a.NumberOfHours == -7));
}

[Test]
public void UnaryPlus()
{
// Ensure expression tree contains UnaryPlus
var param = Expression.Parameter(typeof(TimesheetEntry), "e");
var expr = Expression.Equal(Expression.UnaryPlus(Expression.PropertyOrField(param, "NumberOfHours")), Expression.Constant(7));
var predicate = Expression.Lambda<Func<TimesheetEntry, bool>>(expr, param);
Assert.AreEqual(1, session.Query<TimesheetEntry>().Count(predicate));
}
}
}
5 changes: 5 additions & 0 deletions src/NHibernate/Hql/Ast/HqlTreeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public HqlDivide Divide(HqlExpression lhs, HqlExpression rhs)
return new HqlDivide(_factory, lhs, rhs);
}

public HqlNegate Negate(HqlExpression expression)
{
return new HqlNegate(_factory, expression);
}

public HqlDot Dot(HqlExpression lhs, HqlExpression rhs)
{
return new HqlDot(_factory, lhs, rhs);
Expand Down
8 changes: 8 additions & 0 deletions src/NHibernate/Hql/Ast/HqlTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ public HqlAdd(IASTFactory factory, HqlExpression lhs, HqlExpression rhs)
}
}

public class HqlNegate : HqlExpression
{
public HqlNegate(IASTFactory factory, HqlExpression expression)
: base(HqlSqlWalker.UNARY_MINUS, "-", factory, expression)
{
}
}

public class HqlBooleanOr : HqlBooleanExpression
{
public HqlBooleanOr(IASTFactory factory, HqlBooleanExpression lhs, HqlBooleanExpression rhs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ protected HqlTreeNode VisitUnaryExpression(UnaryExpression expression)
{
switch (expression.NodeType)
{
case ExpressionType.Negate:
return _hqlTreeBuilder.Negate(VisitExpression(expression.Operand).AsExpression());
case ExpressionType.UnaryPlus:
return VisitExpression(expression.Operand).AsExpression();
case ExpressionType.Not:
return _hqlTreeBuilder.BooleanNot(VisitExpression(expression.Operand).ToBooleanExpression());
case ExpressionType.Convert:
Expand Down