title: Group By in LINQ for different Tables
body: <p>I have a database consisting of a many to many relationship using a junction table a a one to many relationship with one of those tables. </p>
  
  <p><a href="https://i.stack.imgur.com/S7FGR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S7FGR.jpg" alt="enter image description here"></a></p>
  
  <p>I created the following query:</p>
  
  <pre><code>Select Producto.Nombre, count(Producto.Nombre), Orden.Fecha
  From Producto
  INNER JOIN ProductoPedido
  ON Producto.IdProducto = ProductoPedido.ProductoIdProducto
  INNER JOIN Pedido
  ON Pedido.IdPedido = ProductoPedido.PedidoIdPedido
  INNER JOIN Orden
  ON Pedido.OrdenNumeroOrden = Orden.NumeroOrden
  GROUP BY  Producto.Nombre, Orden.Fecha 
  </code></pre>
  
  <p>How do I create that exact same query on my project using LINQ?</p>
  
  <p>I attempted the following but it doesn't work.</p>
  
  <pre><code>var data = (from producto in _context.Producto
              join productopedido in _context.ProductoPedido on producto.IdProducto equals productopedido.ProductoIdProducto
              join pedido in _context.Pedido on productopedido.PedidoIdPedido equals pedido.IdPedido
              join orden in _context.Orden on pedido.OrdenNumeroOrden equals orden.NumeroOrden
              group new { producto.Nombre,  orden.Fecha } by new { producto, orden } into g
                          select new
                          {
                              Producto = g.Key.producto.Nombre,
                              Cantidad = g.Key.producto.Nombre.Count(),
                              Fecha = g.Key.orden.Fecha
                           });
  </code></pre>
  
  <p>It returns <code>ArgumentException: Expression of type</code> whenever I try to access the information in my var.</p>
  
  <pre><code>An unhandled exception occurred while processing the request.   
  ArgumentException: Expression of type  'System.Func`2[Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.Data.Entity.Storage.ValueBuffer,Microsoft.Data.Entity.Storage.ValueBuffer],Microsoft.Data.Entity.Storage.ValueBuffer],Microsoft.Data.Entity.Storage.ValueBuffer],&lt;&gt;f__AnonymousType3`2[System.String,System.Int32]]' cannot be used for parameter of type 'System.Func`2[&lt;&gt;f__AnonymousType2`2[System.String,System.DateTime],&lt;&gt;f__AnonymousType3`2[System.String,System.Int32]]' of method 'System.Collections.Generic.IEnumerable`1[System.Linq.IGrouping`2[&lt;&gt;f__AnonymousType3`2[System.String,System.Int32],&lt;&gt;f__AnonymousType2`2[System.String,System.DateTime]]] _GroupBy[&lt;&gt;f__AnonymousType2`2,&lt;&gt;f__AnonymousType3`2,&lt;&gt;f__AnonymousType2`2](System.Collections.Generic.IEnumerable`1[&lt;&gt;f__AnonymousType2`2[System.String,System.DateTime]], System.Func`2[&lt;&gt;f__AnonymousType2`2[System.String,System.DateTime],&lt;&gt;f__AnonymousType3`2[System.String,System.Int32]], System.Func`2[&lt;&gt;f__AnonymousType2`2[System.String,System.DateTime],&lt;&gt;f__AnonymousType2`2[System.String,System.DateTime]])'
  
  System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method,   ExpressionType nodeKind, Expression arg, ParameterInfo pi)
  </code></pre>