From deff96e68c2fcffc7501f285503b90e34ccf00c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Leb=C3=A9e?= Date: Tue, 6 Mar 2018 18:34:28 -0600 Subject: [PATCH] fix forgot to specify second param. --- PoweredSoft.DynamicLinq.Test/ConstantTests.cs | 73 +++++++++++++++++++ .../PoweredSoft.DynamicLinq.Test.csproj | 1 + PoweredSoft.DynamicLinq/Constants.cs | 3 +- .../Helpers/QueryableHelpers.cs | 5 +- 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 PoweredSoft.DynamicLinq.Test/ConstantTests.cs diff --git a/PoweredSoft.DynamicLinq.Test/ConstantTests.cs b/PoweredSoft.DynamicLinq.Test/ConstantTests.cs new file mode 100644 index 0000000..690a0eb --- /dev/null +++ b/PoweredSoft.DynamicLinq.Test/ConstantTests.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using PoweredSoft.DynamicLinq.Dal.Pocos; +using PoweredSoft.DynamicLinq.Extensions; + +namespace PoweredSoft.DynamicLinq.Test +{ + internal class ConstantTestClass + { + public int Id { get; set; } + public int? ForeignKey { get; set; } + public string Text { get; set; } + } + + [TestClass] + public class ConstantTests + { + internal List Posts { get; set; } = new List() + { + new ConstantTestClass { Id = 1, ForeignKey = null, Text = "Hello" }, + new ConstantTestClass { Id = 2, ForeignKey = 1, Text = "Hello 2" }, + new ConstantTestClass { Id = 3, ForeignKey = 2, Text = "Hello 3" }, + new ConstantTestClass { Id = 4, ForeignKey = null, Text = "Hello 4" }, + }; + + [TestMethod] + public void LeaveAsIs() + { + try + { + Posts + .AsQueryable() + .Query(t => t.Equal("ForeignKey", 1, QueryConvertStrategy.LeaveAsIs)); + + Assert.Fail("Should have thrown an exception"); + } + catch(Exception ex) + { + + } + + Assert.IsTrue(Posts.AsQueryable().Query(t => t.Equal("Id", 1, QueryConvertStrategy.LeaveAsIs)).Any()); + } + + [TestMethod] + public void SpecifyType() + { + Assert.IsTrue(Posts.AsQueryable().Query(t => t.Equal("ForeignKey", 1, QueryConvertStrategy.SpecifyType)).Any()); + Assert.IsTrue(Posts.AsQueryable().Query(t => t.Equal("Id", 1, QueryConvertStrategy.SpecifyType)).Any()); + + try + { + Posts.AsQueryable().Query(t => t.Equal("Id", "1", QueryConvertStrategy.SpecifyType)); + Assert.Fail("Should have thrown an exception"); + } + catch + { + + } + } + + [TestMethod] + public void ConvertConstantToComparedPropertyOrField() + { + Assert.IsTrue(Posts.AsQueryable().Query(t => t.Equal("ForeignKey", 1, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)).Any()); + Assert.IsTrue(Posts.AsQueryable().Query(t => t.Equal("ForeignKey", "1", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)).Any()); + Assert.IsTrue(Posts.AsQueryable().Query(t => t.Equal("Id", 1, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)).Any()); + Assert.IsTrue(Posts.AsQueryable().Query(t => t.Equal("Id", "1", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)).Any()); + } + } +} diff --git a/PoweredSoft.DynamicLinq.Test/PoweredSoft.DynamicLinq.Test.csproj b/PoweredSoft.DynamicLinq.Test/PoweredSoft.DynamicLinq.Test.csproj index fc6f2cb..3e87cb5 100644 --- a/PoweredSoft.DynamicLinq.Test/PoweredSoft.DynamicLinq.Test.csproj +++ b/PoweredSoft.DynamicLinq.Test/PoweredSoft.DynamicLinq.Test.csproj @@ -61,6 +61,7 @@ + diff --git a/PoweredSoft.DynamicLinq/Constants.cs b/PoweredSoft.DynamicLinq/Constants.cs index c4f19d5..4f37012 100644 --- a/PoweredSoft.DynamicLinq/Constants.cs +++ b/PoweredSoft.DynamicLinq/Constants.cs @@ -23,7 +23,8 @@ namespace PoweredSoft.DynamicLinq public enum QueryConvertStrategy { LeaveAsIs, - ConvertConstantToComparedPropertyOrField + ConvertConstantToComparedPropertyOrField, + SpecifyType } public enum QueryCollectionHandling diff --git a/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs b/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs index 0eeacd2..f3a3a45 100644 --- a/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs +++ b/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs @@ -81,7 +81,7 @@ namespace PoweredSoft.DynamicLinq.Helpers // attempt a conversion. object convertedValue = TypeHelpers.ConvertFrom(memberType, value); - return Expression.Constant(convertedValue); + return Expression.Constant(convertedValue, memberType); } public static ConstantExpression ResolveConstant(Expression member, object value, QueryConvertStrategy convertStrategy) @@ -89,6 +89,9 @@ namespace PoweredSoft.DynamicLinq.Helpers if (convertStrategy == QueryConvertStrategy.LeaveAsIs) return Expression.Constant(value); + if (convertStrategy == QueryConvertStrategy.SpecifyType) + return Expression.Constant(value, member.Type); + if (convertStrategy == QueryConvertStrategy.ConvertConstantToComparedPropertyOrField) return QueryableHelpers.GetConstantSameAsLeftOperator(member, value);