Forcing LINQ to SQL to make one single call for all child rows

Posted by zaph0d on Stack Overflow See other posts from Stack Overflow or by zaph0d
Published on 2010-05-24T11:31:35Z Indexed on 2010/05/24 11:41 UTC
Read the original article Hit count: 159

Filed under:

Let say I have a method (example taken from another post):

public IQueryable<CityBlock> GetCityBlocks(){
    var results = from o in db.city_blocks
                  let buildings = GetBuildingsOnBlock(o.block_id) //returns Iqueryable
                  select new CityBlock {
                      BuildingsOnBlock = buildings,
                      BlockOwner = o.block_owner
                  };
    return results;
}

In the calling method I add Skip() and Take() methods plus some filtering and then do a ToList().

The trouble is that I am getting dozens of database calls - one for all the city blocks and then a separate one for each building.

Is there a way that I can refactor this code to just make two calls: one for the city blocks and one for all the buildings

© Stack Overflow or respective owner

Related posts about linq-to-sql