fix forgot to specify second param.

This commit is contained in:
David Lebée 2018-03-06 18:34:28 -06:00
parent 667f306598
commit deff96e68c
4 changed files with 80 additions and 2 deletions

View File

@ -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<ConstantTestClass> Posts { get; set; } = new List<ConstantTestClass>()
{
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());
}
}
}

View File

@ -61,6 +61,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ComplexQueriesTests.cs" /> <Compile Include="ComplexQueriesTests.cs" />
<Compile Include="ConstantTests.cs" />
<Compile Include="Helpers\QueryableAssert.cs" /> <Compile Include="Helpers\QueryableAssert.cs" />
<Compile Include="ShortcutTests.cs" /> <Compile Include="ShortcutTests.cs" />
<Compile Include="SimpleQueriesTest.cs" /> <Compile Include="SimpleQueriesTest.cs" />

View File

@ -23,7 +23,8 @@ namespace PoweredSoft.DynamicLinq
public enum QueryConvertStrategy public enum QueryConvertStrategy
{ {
LeaveAsIs, LeaveAsIs,
ConvertConstantToComparedPropertyOrField ConvertConstantToComparedPropertyOrField,
SpecifyType
} }
public enum QueryCollectionHandling public enum QueryCollectionHandling

View File

@ -81,7 +81,7 @@ namespace PoweredSoft.DynamicLinq.Helpers
// attempt a conversion. // attempt a conversion.
object convertedValue = TypeHelpers.ConvertFrom(memberType, value); 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) public static ConstantExpression ResolveConstant(Expression member, object value, QueryConvertStrategy convertStrategy)
@ -89,6 +89,9 @@ namespace PoweredSoft.DynamicLinq.Helpers
if (convertStrategy == QueryConvertStrategy.LeaveAsIs) if (convertStrategy == QueryConvertStrategy.LeaveAsIs)
return Expression.Constant(value); return Expression.Constant(value);
if (convertStrategy == QueryConvertStrategy.SpecifyType)
return Expression.Constant(value, member.Type);
if (convertStrategy == QueryConvertStrategy.ConvertConstantToComparedPropertyOrField) if (convertStrategy == QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)
return QueryableHelpers.GetConstantSameAsLeftOperator(member, value); return QueryableHelpers.GetConstantSameAsLeftOperator(member, value);