I am trying to create a multiple include method in my repository to use as follows:
repository.Include<Post>(x => x.Images, x => x.Tags).First(x => x.Id == 1)
I tried something as:
public IQueryable<T> Include<T>(params Expression<Func<T, Object>>[] paths) where T : class {
return paths.Aggregate(_context.Set<T>(), (x, path) => x.Include(path));
} // Include
But I get the error:
Cannot implicitly convert type ‘System.Linq.IQueryable’ to ‘System.Data.Entity.DbSet’.
Note that the original include is the following:
public static IQueryable Include(
this IQueryable source,
Expression> path
) where T : class;
Can I make this work without turning my repository method into static?
Thank You,
Miguel
If you really want to create your own
.Includenon-extension method that allows for multiple paths in one call, internally translating to the already provided.Includemethod, you can do something likeThis is pretty close to what you have already, but avoids the pitfall with
Enumerable.Aggregatethat you encountered: you’d have the same problem if you replaceIQueryable<T> querywithvar queryin my version.Note that using many
.Includemay harm performance. If it does in your situation, you can rewrite it to use multiple queries, which you can run one after the other in a transaction.Personally, as you can call the already provided
.Includeextension method (using System.Data.Entity;) on anyIQueryable, I’d just write it as: