1

I am having some problems with Entity Framework. I have simplified this to make it easier to explain.

These are my mssql tables mssql tables

I use the following code to get all cities for each of the countries in my MSSQL database

var country = new Country()
{
    Cities = obj.Counties.SelectMany(e => e.Cities).Select(city => new DCCity
    {
        Name = city.Name,
        Population = city.Population
    })
};

This is returned as json

enter image description here

There is a bit more then 40.000 records in the city table. To retrieve a list with all the countries and their respective cities it takes around 8 seconds. I am trying to reduce this. Anyone know some optimization tips to achieve this?

8
  • Please post your full code. The snippet you've provided wouldn't produce that output. Commented Oct 17, 2014 at 18:46
  • Use the SQL Server Profiler to see what SQL is being sent to your database server. Commented Oct 17, 2014 at 18:46
  • Do you really need all 40k records? Commented Oct 17, 2014 at 18:47
  • I supose you make separate query for each county of each country Commented Oct 17, 2014 at 18:48
  • 1
    Do you run one query for each country rather than running one query to get all the data? Where does the JSON come into this, do you send the result as JSON somewhere, and is it possibly that which is taking a long time rather than the database query? How do you create the JSON, could that be the bottle neck? Commented Oct 17, 2014 at 18:51

2 Answers 2

1

You need to query the Cities table first to get all data:

var cities = _context.Cities.Select(x => new {
    ContryId = x.County.Country.CountryId,
    ContryName = x.County.Country.Name,
    CityId = x.Id,
    CityName = x.Name
});

var countryLookup = new Dictionary<int, CountryDto>(approximatelyCountOfCountries);

foreach (var city in cities)
{
    CountryDto country;
    if (!countryLookup.TryGetValue(city.CountryId, out country))
    {
        country = new CountryDto {
            Name = city.CountryName,
            Id = city.CountryId
            Cities = new List<CityDto>(approximatelyCountOfCities)
        };
        countryLookup.Add(country.Id, country);
    }
    country.Cities.Add(new CityDto { Name = city.Name, Id = city.Id });
}

In this way the result will be the:

countryLookup.Values
Sign up to request clarification or add additional context in comments.

Comments

0

Try to do somthing like this:

            var result = from c in countries
                     join conty in counties on c.id equals conty.CountryId
                     join city in cities on conty.id equals city.CountyId

                     group city by c.Name into g

                     select new
                     {
                         Name = g.Key,
                         Cities = g.Select(x =>
                             new
                             {
                                 x.Name,
                                 x.Population
                             })
                     };

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.