1

I have the following model:

public class Order 
{
    public int Id {get; set;}
    public int Number {get; set;}
    public virtual ICollection<OrderDetail> Details {get; set;}
}

public class OrderDetail
{
    public int Id {get; set;}
    public int OrderId {get; set;}
    public virtual Product Product {get; set;}
}

public class Product
{
    public int Id {get; set;}
    public int Number {get; set;}
    public string Description {get; set;}
}

In my OrderRepository, I load a complete order like this:

public override Order Get(int id)
{
    return base.Get(id, x => x.Details);
}

And the base method is:

public virtual T Get(int id, params Expression<Func<T, object>>[] include)
{
    if (include.Any())
    {
        var set = include.Aggregate<Expression<Func<T, object>>, IQueryable<T>>
                  (dbSet, (current, expression) => current.Include(expression));

        return set.SingleOrDefault<T>(x => x.Id == id);
    }

    return dbSet.Find(id);
}

The above works partially fine because it loads the Order and the OrderDetails. However, I also want to load the related Product for every Detail so that I can display the product description in the list too.

How can I improve on the above method to allow me to do so?

1 Answer 1

1

Just add another argument with nested Select:

public override Order Get(int id)
{
    return base.Get(id, x => x.Details, x => x.Details.Select(z => z.Product));
}

See MSDN

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

1 Comment

Wow! Couldn't have been easier!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.