I'm reviewing old questions (by me) without answers, the answer to this is/was:
I should have included the AsJson() code:
public dynamic AsJson()
{
return new
{
name = this.Name,
membersCount = this.Members.Count()
}
}
The query was this:
dynamic results = from g in groups.ToList()
select g.AsJson();
The "this.Members.Count()" is run in memory because of the ToList().
So: the query is divided in to parts: SQL To Entities and SQL To Objects
The first part was fast, the second was slow (has to do Members.Count() N times, being N the number of results in "groups.ToList()"