Best way to join content parts, records?

Topics: General
Feb 7, 2013 at 6:59 PM
Hi guys - I have a - I think - fairly straightforward scenario that I think I am overcomplicating, or missing out on a better way to do what I am doing.

SET UP: we have a LocationPart (and Record) that is joined to a LeaguePart (and record) in a many-to-many relationship via "LocationLeagueRecord", a joining class that is not a content part record. Locations and Leagues have navigation properties for each other.

The twist is, Locations can have a flag set ("IsIncludedInAllLeagues") that overrides the many-to-many mapping. I want a query that returns all of the locations for a given league (LeagueId really): the Locations navigation property of Leagues, PLUS all the Locations where the flag is set to true.

1 - Is there a straightforward query I can use to do this?
2 - What is the best / recommended way to cast the ContentPartRecord to it's related ContentPart? I have a helper that does this:
public static T GetPartById<T>(IContentManager manager, int id)
      where T : ContentPart
      return manager.Get<T>(id);
.. but I'm worried that will send result in multi database calls for a good-sized sequence.

This what I have now, it seems overly complex:
public IEnumerable<EventLocationPart> GetLocations(int leagueId)

      // have to treat seperately - needs to include the "IncludeInAll" items
      var leaguePart = GetLeaguePart(leagueId);

      var query = _contentManager.Query<EventLocationPart>(VersionOptions.Published);
      var alwaysIncludedList = query
        .Where(l => l.IsIncludedInAllLeagues);

      // get these as content parts:
      IEnumerable<EventLocationPart> assignedList = leaguePart.Locations
        .Select(l => ContentManagerHelpers.GetPartById<EventLocationPart>(_contentManager, l.ContentItemRecord.Id));

      IEnumerable<EventLocationPart> list = alwaysIncludedList.Union(assignedList).OrderBy(l => l.As<TitlePart>());

      return list;
Thanks for any help / guidance!