Entity Framework Core 2.0: Error when enumerating over query with group

entity-framework entity-framework-core


I have the following method which simply iterates over orders grouped by client ID.

static void LinqWithInto()

    using (var db = new EFContext())
        var orders = db.Orders.Include(o => o.Client);

        var query = from order in orders
                    group order by order.ClientId into g
                    select new { ClientId = g.Key, Count = g.Count(), Orders = g };

        foreach (var group in query)
            WriteLine($"Client Id: {group.ClientId}, Number of orders: {group.Count}");
            foreach (var order in group.Orders)
                WriteLine($"\tOrder Id: {order.OrderId}, Client Id: {order.Client.ClientId}, Client Name: " +
                    $"{order.Client.Name} Payment: {order.Payment}");


The query fetches orders with associated clients:

public class Order
    public int OrderId { get; set; }
    public int ClientId { get; set; }
    public double Payment { get; set; }

    public Client Client { get; set; }

public class Client
    public int ClientId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

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

The code works well in EF6, but in EF Core I get the following error (the variable query in foreach loop is highlighted):

System.ArgumentException: 'Expression of type 'System.Object' cannot be used for parameter of type 'Microsoft.EntityFrameworkCore.Metadata.IEntityType' of method 'Void StartTracking(System.Object, Microsoft.EntityFrameworkCore.Metadata.IEntityType)''

I wonder what's wrong here?

11/11/2017 2:25:36 PM

Popular Answer

This seems to be a bug in EF Core 2.0 which got addressed in this issue (Contains workaround):

You can get the testfeed 2.0.3 here (Contains fix):

11/17/2017 8:07:52 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow