Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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 src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ public static IQueryable GroupJoin([NotNull] this IQueryable outer, [CanBeNull]
typeof(Queryable), nameof(Queryable.GroupJoin),
new[] { outer.ElementType, innerType, outerSelectorLambda.Body.Type, resultSelectorLambda.Body.Type },
outer.Expression,
Expression.Constant(inner),
inner.AsQueryable().Expression,
Expression.Quote(outerSelectorLambda),
Expression.Quote(innerSelectorLambda),
Expression.Quote(resultSelectorLambda)));
Expand Down
4 changes: 4 additions & 0 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,10 @@ Type FindType(string name)
{
return _root.Type;
}
if (this._parsingConfig.AllowNewToEvaluateAnyType && Type.GetType(name) != null)
{
return Type.GetType(name);
}

return null;
}
Expand Down
5 changes: 5 additions & 0 deletions src/System.Linq.Dynamic.Core/ParsingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@ public IDynamicLinkCustomTypeProvider CustomTypeProvider
/// See https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.1#linq-groupby-translation
/// </summary>
public bool EvaluateGroupByAtDatabase { get; set; }

/// <summary>
/// Allows the New() keyword to evaluate any available Type
/// </summary>
public bool AllowNewToEvaluateAnyType { get; set; } = false;
}
}
37 changes: 37 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/QueryableTests.Select.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This namespace is not available in the dotnet core version from the tests, so this fails.

using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
using Linq.PropertyTranslator.Core;
Expand Down Expand Up @@ -169,6 +170,16 @@ public class Example
public DayOfWeek DOW { get; set; }
public int Sec { get; set; }
public int? SecNull { get; set; }

public class NestedDto
{
public string Name { get; set; }

public class NestedDto2
{
public string Name2 { get; set; }
}
}
}

public class ExampleWithConstructor
Expand Down Expand Up @@ -227,6 +238,32 @@ public void Select_Dynamic_IntoTypeWithNullableProperties2()
Check.That(resultDynamic.Last()).Equals(result.Last());
}

[Fact]
public void Select_Dynamic_IntoKnownNestedType()
{
// Act
IQueryable<string> data = new List<string>() { "name1", "name2" }.AsQueryable();
IQueryable<Example.NestedDto> projectedData =
(IQueryable<Example.NestedDto>) data.Select($"new {typeof(Example.NestedDto).FullName}(~ as Name)");

// Assert
Check.That(projectedData.First().Name).Equals("name1");
Check.That(projectedData.Last().Name).Equals("name2");
}

[Fact]
public void Select_Dynamic_IntoKnownNestedTypeSecondLevel()
{
// Act
IQueryable<string> data = new List<string>() { "name1", "name2" }.AsQueryable();
IQueryable<Example.NestedDto.NestedDto2> projectedData =
(IQueryable<Example.NestedDto.NestedDto2>)data.Select($"new {typeof(Example.NestedDto.NestedDto2).FullName}(~ as Name)");

// Assert
Check.That(projectedData.First().Name).Equals("name1");
Check.That(projectedData.Last().Name).Equals("name2");
}

[Fact]
public void Select_Dynamic_Exceptions()
{
Expand Down