How to do GroupBy on complex object with an IQueryable

Viewed 848

I'm looking for a way to do a GroupBy on a complex object, instead of just one property. The trouble is that I want to do this on an IQueryable, because getting all the data from the table is a really bad idea in this case.

We're using Entity Framework 6.1.

The class looks like this:

 public class Pin {
   public Guid Id {get;set;}
   public Guid PageId {get;set;} /* this is the foreign key to our Pages-table */
   public PageClass Page {get;set;} /* this is a relation */
 }

I need to report on the times a certain page has been "pinned", printing the name of the page as well.

Right now my code looks like this:

var pinnedPages = GetAll().GroupBy(x => x, comparer);

foreach (var pinnedPage in pinnedPages)
{
    var numberOfTimesPinned = pinnedPage.Count();
    var pin = pinnedPage.Key;

    //write a line to the report
 }

But if I group on PageId, the pinnedPage.Key returns a Guid, obviously, while I need the whole Page object for my reporting needs.

I have tried implementing a custom comparer as well, but this cannot be translated to SQL, obviously which is why this doesn't work either.

2 Answers
Related