Question

I use PredicateBuilder to pass a Dynamic Expression to a method in my repository that uses it within Where clause. Here is the relevant repository code:

        var results = context.Set<T>().AsExpandable.Where(where);


        foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            results = results.Include(includeProperty);
        }
        return results.ToList();

In my controller I have this:

var predicate = PredicateBuilder.True<Account>();
if (amount> 0)
    predicate = predicate.And(d => d.Amount <= amount);


var model = AccountRepository.Get(predicate, "Owner").ToList();
return PartialView(model);

When I make a reference to a property of "Owner" it makes another roundtrip to the database, causing an n+1 query problem. Is there any workaround to avoid this?

Était-ce utile?

La solution

Do the includes before the AsExpandable like so:

var results = context.Set<T>()

foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
    results = results.Include(includeProperty);
}

return results.AsExpandable.Where(where).ToList();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top