Uploaded image for project: 'NHibernate'
  1. NHibernate
  2. NH-3027

Linq group by entity and returning the entity fails with wrong SQL

    Details

    • Type: Bug
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 3.2.0.GA
    • Fix Version/s: None
    • Component/s: Linq Provider

      Description

      The following test is a variation of the test given in the first comment on NH-2560 Closed . This test fails, because the generated SQL includes all properties from the customer in the select clause, which of course isn't allowed since we are only grouping on the customer's id. Not sure if this is an important case, but to get it working would require a subquery to be generated.

      [Test]
      public void SingleKeyPropertyGroupByEntityAndSelectEntity()
      {
      	var orderCounts = db.Orders
      		.GroupBy(o => o.Customer)
      		.Select(g => new { Customer = g.Key, OrderCount = g.Count() })
      		.OrderByDescending(t => t.OrderCount)
      		.ToList();
      
      	AssertOrderedBy.Descending(orderCounts, oc => oc.OrderCount);
      }
      

      The flawed SQL:

          select
              customer1_.CustomerId as col_0_0_,
              cast(count(*) as INT) as col_1_0_,
              customer1_.CustomerId as CustomerId27_,
              customer1_.CompanyName as CompanyN2_27_,
              customer1_.ContactName as ContactN3_27_,
              customer1_.ContactTitle as ContactT4_27_,
              customer1_.Address as Address27_,
              customer1_.City as City27_,
              customer1_.Region as Region27_,
              customer1_.PostalCode as PostalCode27_,
              customer1_.Country as Country27_,
              customer1_.Phone as Phone27_,
              customer1_.Fax as Fax27_ 
          from
              Orders order0_ 
          left outer join
              Customers customer1_ 
                  on order0_.CustomerId=customer1_.CustomerId 
          group by
              customer1_.CustomerId 
          order by
              cast(count(*) as INT) desc
      

        Attachments

          Issue links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                oskar.berggren Oskar Berggren
              • Votes:
                15 Vote for this issue
                Watchers:
                14 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Who's Looking?