kilotaras
kilotaras

Reputation: 1429

Explicit loading for computed property

Suppose that in my model I have a computed property A that depends on B and C.D.

Is it possible to make

.Include("A")

be equal to

.Include("B").Include("C.D")

in my linq to entity expressions?

Upvotes: 1

Views: 62

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

No, it's not possible. Include specifies related objects to include in the query results (i.e. tables to join). Thus A is not a related object, but a simple computed property, then it's not possible to include it.


Inspired by @CodeCaster - if you don't want to create repository for this entity, you can write extension method which will do the including

public static IQueryable<Foo> IncludeA(this DbSet<Foo> foos)
{
    return foos.Include("B").Include("C.D");
}

Usage:

db.Foos.IncludeA()

Upvotes: 3

CodeCaster
CodeCaster

Reputation: 151738

This is not possible with Entity Framework by default, but if you wrap your actual entity (say, X) in some sort of Entity Framework-specific repository that executes Include()s according to the situation, you could end up with something like this:

public class XRepository
{
    private IDbSet<X> _dbSet;

    public IQueryable<X> Include()
    {
        return _dbSet;
    }

    public IQueryable<X> IncludeForA()
    {
        return Include().Include("B")
                        .Include("C.D")
    }
}

Then from wherever you want to access a collection of X with B and C.D included, call the IncludeForA() method.

Upvotes: 3

Related Questions