locked
Eager load a graph of an abstract entity? RRS feed

  • Question

  • Here is my model:

    I want to implement this method in the domain service:

    public IQueryable<Transaction> GetProjectTransactions(int projectId)
    {
      var transactions = DbContext.Transactions;
      var incomes = transactions.OfType<ProjectIncome>().Where(pi => pi.ProjectId == projectId);
      var expenses = transactions.OfType<ProjectExpense>().Where(pi => pi.ProjectId == projectId).Cast<Transaction>();
      return expenses.Concat(incomes);
    }


    The function above works great.
    My issue is, I want to eager-include the Investor navigation property of ProjectInvestment. Is there a way to do this?


    Note that Transaction is abstract, but Income, and ProjectIncome are not abstract and are also used as entities for themselves.

    Here are my attempts:

    var incomes = transactions.OfType<ProjectIncome>().Where(pi => pi.ProjectId == projectId);
    var projectInvestments = incomes.OfType<ProjectInvestment>().Include(pi => pi.Investor);
    var expenses = transactions.OfType<ProjectExpense>().Where(pi => pi.ProjectId == projectId).Cast<Transaction>();
    return expenses.Concat(incomes.Except(projectInvestments)).Concat(projectInvestments);

    Exception:
    A specified Include path is not valid. The EntityType 'Model.ProjectIncome' does not declare a navigation property with the name 'Investor'.

    The problem occurs while trying to combine the ProjectInvestment with the ProjectIncome (ProjectInvestment's baseclass) results, executing the following throws the same exception:

    var result = incomes.Concat(projectInvestments).ToArray();

    A temporary hack, even ugly will also be great. I just want to keep the WCF API as it is, and not break the query in two methods (Executing the query on server and returning the objects is too ugly if you gave it a thought).


    Shimmy

    Thursday, March 22, 2012 9:43 AM

Answers