I have this query witch is working fine :
List<IGrouping<Country, VisitedCity>> tempQuery = null;
using (var db = new MyDataContext())
{
tempQuery = db.VisitedCities.Include(c => c.PersonWhoVisited).Include(c => c.PersonWhoVisitedNationality).Include(c => c.City)
.GroupBy(c => c.PersonWhoVisitedNationality)
.ToList();
}
var dataInput = tempQuery
.OrderBy(c => c.Key.Name)
.Select(cp => new
{
CountryName = cp.Key.Name,
VisitationsByCity = cp
.GroupBy(x => x.City, x => x.CityId)
.Select(c => new
{
City = c.Key,
NumberOfVisits = c.Count()
})
}).ToList();
But the problem is that it is loading all data to my application (i have now 300 000 rows already on my largest table) and its getting slow day by day of course because its loading all in the ToList() method call.
I have this splitted in two calls because i cannot figure out how to make a single call to the database and return only the dataInput, if i merge both calls into one i get "Object reference not set to an instance of an object." exeption, this is probably because some references are not being included, but i cannot figure out what more tables i include in the query...
also im using entity framework 7 which still doesn't support lazy loading and has some features missing still, but this should be possible right? In the includes i tried using a select statement
.Include(c => c.LadiesInWaiting.Select(b => b.Princess))
like mentioned on here : http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx
but is not recognized (because its new entity framework 7 ?)
Update : Ok if i use the .AsEnumerable i can make all in a single query, but still it seems to take for about 6 seconds to load the data in the next call and also loads 250 mb worth of memory at this instance of time ...
var tempQuery = db.VisitedCities.Include(c => c.PersonWhoVisitedNationality).Include(c => c.City)
.GroupBy(c => c.PersonWhoVisitedNationality)
.AsEnumerable()
.OrderBy(c => c.Key.Name)
.Select(cp => new
{
CountryName = cp.Key.Name,
VisitationsByCity = cp
.GroupBy(x => x.City, x => x.CityId)
.Select(c => new
{
City = c.Key,
NumberOfVisits = c.Count()
})
});
var allCities1 = tempQuery
.SelectMany(x => x.VisitationsByCity.Select(c => c.City))
.Distinct().OrderBy(city => city.Name).ToList();