Skip to content

Commit 5a89128

Browse files
committed
Fix array projection
Fixes #278 (cherry picked from commit f27eeab)
1 parent 165b06d commit 5a89128

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/EFCore.PG/Query/ExpressionVisitors/NpgsqlSqlTranslatingExpressionVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected override Expression VisitSubQuery(SubQueryExpression expression)
4040
if (properties.Count == 0)
4141
return null;
4242
var lastPropertyType = properties[properties.Count - 1].ClrType;
43-
if (lastPropertyType.IsArray && lastPropertyType.GetArrayRank() == 1)
43+
if (lastPropertyType.IsArray && lastPropertyType.GetArrayRank() == 1 && subQueryModel.ResultOperators.Count > 0)
4444
{
4545
// Translate someArray.Length
4646
if (subQueryModel.ResultOperators.First() is CountResultOperator)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.EntityFrameworkCore.Utilities;
7+
using Npgsql.EntityFrameworkCore.PostgreSQL.FunctionalTests;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace Microsoft.EntityFrameworkCore.Query
12+
{
13+
public class QueryBugsTest : IClassFixture<NpgsqlFixture>
14+
{
15+
// ReSharper disable once UnusedParameter.Local
16+
public QueryBugsTest(NpgsqlFixture fixture, ITestOutputHelper testOutputHelper)
17+
{
18+
Fixture = fixture;
19+
//Fixture.TestSqlLoggerFactory.Clear();
20+
//Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper);
21+
}
22+
23+
protected NpgsqlFixture Fixture { get; }
24+
25+
[Fact]
26+
public async Task Bug278()
27+
{
28+
using (var testStore = NpgsqlTestStore.CreateScratch())
29+
using (var context = new Bug278Context(new DbContextOptionsBuilder()
30+
.UseNpgsql(testStore.Connection)
31+
.Options))
32+
{
33+
context.Database.EnsureCreated();
34+
context.Entities.Add(new Bug278Entity { ChannelCodes = new[] { 1, 1 } });
35+
context.SaveChanges();
36+
37+
var actual = await context.Entities.Select(x => new
38+
{
39+
Codes = x.ChannelCodes.Select(c => (ChannelCode)c)
40+
}).FirstOrDefaultAsync();
41+
42+
Assert.Equal(new[] { ChannelCode.Code, ChannelCode.Code }, actual.Codes);
43+
}
44+
}
45+
46+
public enum ChannelCode { Code = 1 }
47+
48+
public class Bug278Entity
49+
{
50+
public int Id { get; set; }
51+
public int[] ChannelCodes { get; set; }
52+
}
53+
54+
class Bug278Context : DbContext
55+
{
56+
public Bug278Context(DbContextOptions options) : base(options) {}
57+
public DbSet<Bug278Entity> Entities { get; set; }
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)