mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-06-11 01:38:18 +00:00
Support for projectable generic methods
This commit is contained in:
+1
-1
@@ -63,4 +63,4 @@ namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace EntityFrameworkCore.Projectables.FunctionalTests.Generics
|
||||
{
|
||||
public record Entity : IEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.FunctionalTests.Generics
|
||||
{
|
||||
public static class EntityExtensions
|
||||
{
|
||||
[Projectable]
|
||||
public static TEntity? DefaultIfIdIsNegative<TEntity>(this TEntity entity) where TEntity : IEntity
|
||||
=> entity.Id >= 0 ? entity : default;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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.Generics
|
||||
{
|
||||
[UsesVerify]
|
||||
public class GenericFunctionTests
|
||||
{
|
||||
[Fact]
|
||||
public Task DefaultIfIdIsNegative()
|
||||
{
|
||||
using var context = new SampleDbContext<Entity>();
|
||||
var query = context.Set<Entity>()
|
||||
.Select(x => x.DefaultIfIdIsNegative());
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
SELECT CASE
|
||||
WHEN [e].[Id] >= 0 THEN CAST(1 AS bit)
|
||||
ELSE CAST(0 AS bit)
|
||||
END, [e].[Id], [e].[Name]
|
||||
FROM [Entity] AS [e]
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
SELECT [e].[Id]
|
||||
FROM [Entity] AS [e]
|
||||
ORDER BY [e].[Id]
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace EntityFrameworkCore.Projectables.FunctionalTests.Generics
|
||||
{
|
||||
public interface IEntity
|
||||
{
|
||||
int Id { get; }
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
SELECT CAST(1 AS bit), [e].[Id], [e].[Name]
|
||||
FROM [Entity] AS [e]
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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.Generics
|
||||
{
|
||||
[UsesVerify]
|
||||
public class MultipleGenericsTests
|
||||
{
|
||||
[Projectable]
|
||||
public static object? Coalesce<T1, T2>(T1? t1, T2 t2)
|
||||
=> t1 != null ? t1 : t2;
|
||||
|
||||
[Fact]
|
||||
public Task TestMultipleArguments()
|
||||
{
|
||||
using var context = new SampleDbContext<Entity>();
|
||||
|
||||
var query = context.Set<Entity>()
|
||||
.Select(x => Coalesce(x.Id, x.Name));
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void MultipleInvocations()
|
||||
{
|
||||
using var context = new SampleDbContext<Entity>(Infrastructure.CompatibilityMode.Full);
|
||||
|
||||
var query1 = context.Set<Entity>()
|
||||
.Select(x => Coalesce(x.Id, x.Name))
|
||||
.ToQueryString();
|
||||
|
||||
var query2 = context.Set<Entity>()
|
||||
.Select(x => Coalesce(x.Name, x.Id))
|
||||
.ToQueryString();
|
||||
|
||||
Assert.NotEqual(query1, query2);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user