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

GroupJoin clause is not working - EFCore throws an exception #27858

Closed
Sergio1C opened this issue Apr 21, 2022 · 2 comments
Closed

GroupJoin clause is not working - EFCore throws an exception #27858

Sergio1C opened this issue Apr 21, 2022 · 2 comments

Comments

@Sergio1C
Copy link

Include your code

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Test
{
    class Program
    {
        static void Main()
        {
            var connection = new Microsoft.Data.Sqlite.SqliteConnection("Data Source=:memory:");
            connection.Open();

            var options = new DbContextOptionsBuilder<TestContext>()
                .UseSqlite(connection)
                .Options;

            using (var dbContext = new TestContext(options))
            {
                dbContext.Database.EnsureCreated();
            }

            using (var dbContext = new TestContext(options))
            {
                var query = dbContext.Orders
                    .GroupJoin(dbContext.BotTasks,
                    o => o.Id,
                    bt => bt.OrderId,
                    (o, bt) => new
                    {
                        Order = o,
                        BotTasks = bt,
                    });

                var result = query.ToList();
            }
        }
    }

    internal class TestContext : DbContext
    {
        public TestContext(DbContextOptions options)
            : base(options)
        {
        }

        public DbSet<Order> Orders { get; set; }

        public DbSet<BotTask> BotTasks { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Order>()
                .HasMany(o => o.BotTasks)
                .WithOne(bt => bt.Order);

            modelBuilder.Entity<Order>().HasData(new Order { Id = 1 });
            modelBuilder.Entity<BotTask>().HasData(
                new BotTask { Id = 1, OrderId = 1 },
                new BotTask { Id = 2, OrderId = 1 });

        }
        
        public class Order
        {
            public int Id { get; set; }
            public virtual ICollection<BotTask> BotTasks { get; set; }
        }

        public class BotTask
        {
            public int Id { get; set; }
            [Required]
            public int OrderId { get; set; }
            public virtual Order Order { get; set; }
        }
    }
}

Include stack traces

at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
   at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryTranslationPreprocessor.Process(Expression query)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Test.Program.Main() in C:\Users\ASUS\Source\Repos\ConsoleApp_Test_Issue\Program.cs:line 39`

Exception output in IDE:

System.InvalidOperationException: 'The LINQ expression 'DbSet<Order>()
    .GroupJoin(
        inner: DbSet<BotTask>(), 
        outerKeySelector: o => o.Id, 
        innerKeySelector: bt => bt.OrderId, 
        resultSelector: (o, bt) => new { 
            Order = o, 
            BotTasks = bt
         })' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

Include provider and version information

EF Core version: 5.0.16
Database provider: Microsoft.EntityFrameworkCore.Sqlite (5.0.16)
Target framework: NET 5.0
Operating system: Windows 10 Home Edition (build 19044.1645)
IDE: Visual Studio 2019 16.11.2

@smitpatel
Copy link
Member

Duplicate of #19930

@smitpatel smitpatel marked this as a duplicate of #19930 Apr 21, 2022
@Sergio1C
Copy link
Author

I didn't expect this from ef core..wasted

@Sergio1C Sergio1C changed the title GroupJoin clause is not working - EFCore throw an exception GroupJoin clause is not working - EFCore throws an exception Apr 24, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants