fix forgot to specify second param.
This commit is contained in:
parent
667f306598
commit
deff96e68c
73
PoweredSoft.DynamicLinq.Test/ConstantTests.cs
Normal file
73
PoweredSoft.DynamicLinq.Test/ConstantTests.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
@ -61,6 +61,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ComplexQueriesTests.cs" />
|
||||
<Compile Include="ConstantTests.cs" />
|
||||
<Compile Include="Helpers\QueryableAssert.cs" />
|
||||
<Compile Include="ShortcutTests.cs" />
|
||||
<Compile Include="SimpleQueriesTest.cs" />
|
||||
|
@ -23,7 +23,8 @@ namespace PoweredSoft.DynamicLinq
|
||||
public enum QueryConvertStrategy
|
||||
{
|
||||
LeaveAsIs,
|
||||
ConvertConstantToComparedPropertyOrField
|
||||
ConvertConstantToComparedPropertyOrField,
|
||||
SpecifyType
|
||||
}
|
||||
|
||||
public enum QueryCollectionHandling
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user