0

In our database we have a large "god" table that has several parent/child relationships with other tables (PK/FK)...and those tables have several parent/child relationships with other tables....and so on. Basically a tree, rooted at the "god" table level. I know this is a common table structure.

We're using Entity Framework's entities to map to the tables. Is there a decent way to use Linq to select the entire record "tree" in one statement? Maybe something along the lines of nesting/chaining the selects to include the child entities/tables, so we return the god object along with all its related objects? Thanks.

UPDATE: OK, it appears using .Include() is really what I'm looking for. Thanks for pointing out the duplicate.

TheDudeDude
  • 123
  • 1
  • 4
  • 15
  • Could you provide us an example schema to make this quest a little more concrete? – Matthew Cox Mar 14 '15 at 21:42
  • Well, I described the schema. It's pretty common. A series of one-to-manys, creating a tree structure of relationships. Similar to a Directory structure, but instead of directories, they're tables.. – TheDudeDude Mar 14 '15 at 22:50

1 Answers1

0

Remember that you can use projection to do all this without resorting to includes which will lead to a slower query. An small example is like this :

var relatiesOverviewsEnumerable = relatieRepository
            .Query()
            .NoTracking()
            .OrderBy(q => q.OrderBy(d => d.RelatieId))
            .Select(b => new RelatieOverview
            {
                RelatieId = b.RelatieId,
                Naam = b.Naam,
                BTW = b.BTW,
                HoofdAdres = b.Adressen.FirstOrDefault(a => a.AdresTypeId == HoofdadresType)
            });
        _relatieOverviews = new ObservableCollection<RelatieOverview>(relatiesOverviewsEnumerable);
Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39