Can I order by a column that is not in the group?

For example, I have a query like this:

var data= from c in customers
          join o in orders
          on c.id=o.id
         group new { c, o } by new { o.customerid, c.FirstName, c.LastName, c.City } into customergroups
         orderby customergroup.select(x=>x.o.quantity)

But it gives me an empty result set.

Please suggest ..

+3
source share
1 answer

Yes, you can order your groups by anyone as long as the expression creates a comparable object. For example, you can order a minimum or maximum quantity * but not just quantity, because a group can have many different quantities.

var data= from c in customers
      join o in orders
      on c.id=o.id
     group new { c, o } by new { o.customerid, c.FirstName, c.LastName, c.City } into customergroups
     orderby customergroup.Max(x=>x.o.quantity);

* You can also use First(x=>x.o.quantity)or Last(x=>x.o.quantity), but the order will be arbitrary.

+2
source

All Articles