Skip to content
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

Fix conversion from a non-nullable value type to the nullable value type #189

Merged
merged 3 commits into from
Jul 27, 2018
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
2 changes: 1 addition & 1 deletion ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
https://github.com/GitTools/GitReleaseNotes

GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.8.11
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.8.12

GitReleaseNotes.exe . /OutputFile CHANGELOG.md /allTags
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Description>Dynamic Linq extensions for EntityFramework which adds Async support</Description>
<AssemblyTitle>EntityFramework.DynamicLinq</AssemblyTitle>
<VersionPrefix>1.0.8.11</VersionPrefix>
<VersionPrefix>1.0.8.12</VersionPrefix>
<Authors>Stef Heyenrath</Authors>
<TargetFrameworks>net45;net46</TargetFrameworks>
<DefineConstants>EF</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<PropertyGroup>
<Description>Dynamic Linq extensions for Microsoft.EntityFrameworkCore which adds Async support</Description>
<AssemblyTitle>Microsoft.EntityFrameworkCore.DynamicLinq</AssemblyTitle>
<VersionPrefix>1.0.8.11</VersionPrefix>
<VersionPrefix>1.0.8.12</VersionPrefix>
<Authors>Stef Heyenrath</Authors>
<TargetFrameworks>net451;net46;netstandard1.3;netstandard2.0;uap10.0</TargetFrameworks>
<TargetFrameworks>net451;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
<DefineConstants>$(DefineConstants);EFCORE</DefineConstants>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Microsoft.EntityFrameworkCore.DynamicLinq</AssemblyName>
Expand Down
21 changes: 19 additions & 2 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,13 +1246,30 @@ private Expression CreateNewExpression(List<DynamicProperty> properties, List<Ex
ConstructorInfo ctor = type.GetConstructor(propertyTypes);
if (ctor != null && ctor.GetParameters().Length == expressions.Count)
{
return Expression.New(ctor, expressions, (IEnumerable<MemberInfo>)propertyInfos);
var expressionsPromoted = new List<Expression>();

// Loop all expressions and promote if needed
for (int i = 0; i < propertyTypes.Length; i++)
{
Type propertyType = propertyTypes[i];
Type expressionType = expressions[i].Type;

// Promote from Type to Nullable Type if needed
expressionsPromoted.Add(ExpressionPromoter.Promote(expressions[i], propertyType, true, true));
}

return Expression.New(ctor, expressionsPromoted, (IEnumerable<MemberInfo>)propertyInfos);
}

MemberBinding[] bindings = new MemberBinding[properties.Count];
for (int i = 0; i < bindings.Length; i++)
{
bindings[i] = Expression.Bind(type.GetProperty(properties[i].Name), expressions[i]);
PropertyInfo property = type.GetProperty(properties[i].Name);
Type propertyType = property.PropertyType;
Type expressionType = expressions[i].Type;

// Promote from Type to Nullable Type if needed
bindings[i] = Expression.Bind(property, ExpressionPromoter.Promote(expressions[i], propertyType, true, true));
}

return Expression.MemberInit(Expression.New(type), bindings);
Expand Down
4 changes: 2 additions & 2 deletions src/System.Linq.Dynamic.Core/System.Linq.Dynamic.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<PropertyGroup>
<Description>This is a .NETStandard/ .NET Core port of the the Microsoft assembly for the .Net 4.0 Dynamic language functionality.</Description>
<AssemblyTitle>System.Linq.Dynamic.Core</AssemblyTitle>
<VersionPrefix>1.0.8.11</VersionPrefix>
<VersionPrefix>1.0.8.12</VersionPrefix>
<Authors>Microsoft;Scott Guthrie;King Wilder;Nathan Arnott;Stef Heyenrath</Authors>
<TargetFrameworks>net35;net40;net45;net46;uap10.0;netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net35;net40;net45;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>System.Linq.Dynamic.Core</AssemblyName>
<AssemblyOriginatorKeyFile>System.Linq.Dynamic.Core.snk</AssemblyOriginatorKeyFile>
Expand Down
66 changes: 66 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/QueryableTests.Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Linq.PropertyTranslator.Core;
using QueryInterceptor.Core;
using Xunit;
using NFluent;
#if EFCORE
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
#else
Expand Down Expand Up @@ -161,6 +162,71 @@ public void Select_Dynamic_IntoType()
Assert.Equal(testList.Select(x => x.Profile).Cast<object>().ToList(), userProfiles.ToDynamicList());
}

public class Example
{
public DateTime Time { get; set; }
public DayOfWeek? DOWNull { get; set; }
public DayOfWeek DOW { get; set; }
public int Sec { get; set; }
public int? SecNull { get; set; }
}

public class ExampleWithConstructor
{
public DateTime Time { get; set; }
public DayOfWeek? DOWNull { get; set; }
public DayOfWeek DOW { get; set; }
public int Sec { get; set; }
public int? SecNull { get; set; }

public ExampleWithConstructor(DateTime t, DayOfWeek? dn, DayOfWeek d, int s, int? sn)
{
Time = t;
DOWNull = dn;
DOW = d;
Sec = s;
SecNull = sn;
}
}

[Fact]
public void Select_Dynamic_IntoTypeWithNullableProperties1()
{
// Arrange
var dates = Enumerable.Repeat(0, 7)
.Select((d, i) => new DateTime(2000, 1, 1).AddDays(i).AddSeconds(i))
.AsQueryable();

// Act
IQueryable<Example> result = dates
.Select(d => new Example { Time = d, DOWNull = d.DayOfWeek, DOW = d.DayOfWeek, Sec = d.Second, SecNull = d.Second });
IQueryable<Example> resultDynamic = dates
.Select<Example>("new (it as Time, DayOfWeek as DOWNull, DayOfWeek as DOW, Second as Sec, int?(Second) as SecNull)");

// Assert
Check.That(resultDynamic.First()).Equals(result.First());
Check.That(resultDynamic.Last()).Equals(result.Last());
}

[Fact]
public void Select_Dynamic_IntoTypeWithNullableProperties2()
{
// Arrange
var dates = Enumerable.Repeat(0, 7)
.Select((d, i) => new DateTime(2000, 1, 1).AddDays(i).AddSeconds(i))
.AsQueryable();

// Act
IQueryable<ExampleWithConstructor> result = dates
.Select(d => new ExampleWithConstructor(d, d.DayOfWeek, d.DayOfWeek, d.Second, d.Second));
IQueryable<ExampleWithConstructor> resultDynamic = dates
.Select<ExampleWithConstructor>("new (it as Time, DayOfWeek as DOWNull, DayOfWeek as DOW, Second as Sec, int?(Second) as SecNull)");

// Assert
Check.That(resultDynamic.First()).Equals(result.First());
Check.That(resultDynamic.Last()).Equals(result.Last());
}

[Fact]
public void Select_Dynamic_Exceptions()
{
Expand Down