mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-05-19 09:41:21 +00:00
support for rewriting null conditional access expressions
This commit is contained in:
+2
-2
@@ -11,8 +11,8 @@ namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
|
||||
[Projectable]
|
||||
public static int Foo(this Entity entity) => entity.Id + 1;
|
||||
|
||||
[Projectable]
|
||||
public static int Foo2(this Entity entity) => entity.Foo() + 1;
|
||||
[Projectable]
|
||||
public static int Foo2(this Entity entity) => entity.Foo() + 1;
|
||||
|
||||
[Projectable]
|
||||
public static Entity? LeadingEntity(this Entity entity, DbContext dbContext)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
|
||||
{
|
||||
public record Entity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public List<Entity>? RelatedEntities { get; set; }
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
#nullable disable
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
|
||||
{
|
||||
public static class EntityExtensions
|
||||
{
|
||||
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
|
||||
public static string GetNameIgnoreNulls(this Entity entity)
|
||||
=> entity?.Name;
|
||||
|
||||
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
|
||||
public static int? GetNameLengthIgnoreNulls(this Entity entity)
|
||||
=> entity.GetNameIgnoreNulls()?.Length;
|
||||
|
||||
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Ignore)]
|
||||
public static Entity GetFirstRelatedIgnoreNulls(this Entity entity)
|
||||
=> entity?.RelatedEntities?[0];
|
||||
|
||||
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||
public static string GetNameRewriteNulls(this Entity entity)
|
||||
=> entity?.Name;
|
||||
|
||||
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||
public static int? GetNameLengthRewriteNulls(this Entity entity)
|
||||
=> entity.GetNameIgnoreNulls()?.Length;
|
||||
|
||||
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
|
||||
public static Entity GetFirstRelatedRewriteNulls(this Entity entity)
|
||||
=> entity?.RelatedEntities?[0];
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT CAST(LEN([e].[Name]) AS int)
|
||||
FROM [Entity] AS [e]
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
SELECT [e].[Id], [e0].[Id], [e0].[EntityId], [e0].[Name]
|
||||
FROM [Entity] AS [e]
|
||||
LEFT JOIN [Entity] AS [e0] ON [e].[Id] = [e0].[EntityId]
|
||||
ORDER BY [e].[Id]
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT [e].[Name]
|
||||
FROM [Entity] AS [e]
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
|
||||
{
|
||||
[UsesVerify]
|
||||
public class IngoreNullConditionalRewriteTests
|
||||
{
|
||||
[Fact]
|
||||
public Task SimpleMemberExpression()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.GetNameIgnoreNulls());
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ComplexMemberExpression()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.GetNameLengthIgnoreNulls());
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task RelationalExpression()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.GetFirstRelatedIgnoreNulls());
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT CAST(LEN([e].[Name]) AS int)
|
||||
FROM [Entity] AS [e]
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
SELECT CAST(1 AS bit), [e].[Id], [e0].[Id], [e0].[EntityId], [e0].[Name], [e1].[Id], [e1].[EntityId], [e1].[Name]
|
||||
FROM [Entity] AS [e]
|
||||
LEFT JOIN [Entity] AS [e0] ON [e].[Id] = [e0].[EntityId]
|
||||
LEFT JOIN [Entity] AS [e1] ON [e].[Id] = [e1].[EntityId]
|
||||
ORDER BY [e].[Id], [e0].[Id]
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT CAST(LEN([e].[Name]) AS int)
|
||||
FROM [Entity] AS [e]
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.FunctionalTests.NullConditionals
|
||||
{
|
||||
[UsesVerify]
|
||||
public class RewriteNullConditionalRewriteTests
|
||||
{
|
||||
[Fact]
|
||||
public Task SimpleMemberExpression()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.GetNameLengthRewriteNulls());
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ComplexMemberExpression()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.GetNameLengthRewriteNulls());
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task RelationalExpression()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Entity>();
|
||||
|
||||
var query = dbContext.Set<Entity>()
|
||||
.Select(x => x.GetFirstRelatedRewriteNulls());
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user