EF Including Other Entities (Generic Repository pattern)

I am using the Generic Repository pattern on top of Entity Framework Code First. Everything was working fine until I needed to include more entities in a query. I got to include one entity successfully, but now I can't figure out how to include multiple entities. Check out what I've got so far:

public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
{
var entityName = GetEntityName<TEntity>();
return _objectContext.CreateQuery<TEntity>(entityName);
}


public IList<TEntity> GetQueryWithInclude<TEntity>(string toInclude) where TEntity : class
{
var entityName = GetEntityName<TEntity>();
return _objectContext.CreateQuery<TEntity>(entityName).Include(toInclude).ToList();
}


private string GetEntityName<TEntity>() where TEntity : class
{
return string.Format("{0}.{1}", _objectContext.DefaultContainerName, _pluralizer.Pluralize(typeof(TEntity).Name));
}

What I tried to do but didn't work was pass in an array of strings into a function, then try to "append" the includes on top of the query. I was wondering what if I called the GetQueryWithInclude and passed an entity name (actually a navigation property) at a time to aggregate the results of the query, but I'm worried this might duplicate the results of the query on each call... What do you think would be the best way to get this to work?

Thanks in advance!

UPDATE:

Here's an example of what I'm trying to achieve:

public IQueryable GetQueryWithIncludes(string[] otherEntities)
{
var entityName = GetEntityName<TEntity>();
//now loop over the otherEntities array
//and append Include extensions to the query
//so inside the loop, something like:
_objectContext.GetQuery<TEntity>(entityName).Include(otherEntities[index]);
}
68150 次浏览

Use just the Include extension on IQueryable. It is available in EF 4.1 assembly. If you don't want to reference that assembly in your upper layers create wrapper extension method in your data access assembly.

Here you have example:

public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query, params Expression<Func<T, object>>[] includes)
where T : class
{
if (includes != null)
{
query = includes.Aggregate(query,
(current, include) => current.Include(include));
}


return query;
}

You will use it for example like:

var query = context.Customers
.IncludeMultiple(
c => c.Address,
c => c.Orders.Select(o => o.OrderItems));

This query will load all customers with their addresses and orders and every order will contain its order items.

//I have included the bare minimum here. Below is how to use it.

     IQueryable<File> xg= UnitOfWork.Files.GetAllLazyLoad(d => d.FileId == 1,
r => r.FileCategory);
//where r.FileCategory is a navigational property.


//Interface




namespace Msh.Intranet.Repository.GenericRepoPattern
{
public interface IRepository<T> where T:class
{


IQueryable<T> GetAllLazyLoad(Expression<Func<T, bool>> filter, params Expression<Func<T, object>>[] children);


}
}






namespace Msh.Intranet.Repository.GenericRepoPattern
{
/// <summary>
/// The EF-dependent, generic repository for data access
/// </summary>
/// <typeparam name="T">Type of entity for this Repository.</typeparam>
public class EFRepository<T> : IRepository<T> where T : class
{
public EFRepository(DbContext dbContext)
{
if (dbContext == null)
throw new ArgumentNullException("dbContext");
DbContext = dbContext;
DbSet = DbContext.Set<T>();


}


protected DbContext DbContext { get; set; }


protected DbSet<T> DbSet { get; set; }




public virtual IQueryable<T> GetAllLazyLoad(Expression<Func<T, bool>> filter, params Expression<Func<T, object>>[] children)
{




children.ToList().ForEach(x=>DbSet.Include(x).Load());
return DbSet;
}


}
}

For entity framework create a list of strings to hold the includes. See the below example code

public virtual IQueryable<TObject> Filter(Expression<Func<TObject, bool>> filter,
int skip = 0,
int take = int.MaxValue,
Func<IQueryable<TObject>, IOrderedQueryable<TObject>> orderBy = null,
IList<string> incudes = null)
{
var _resetSet = filter != null ? DbSet.AsNoTracking().Where(filter).AsQueryable() : DbSet.AsNoTracking().AsQueryable();


if (incudes != null)
{
foreach (var incude in incudes)
{
_resetSet = _resetSet.Include(incude);
}
}
if (orderBy != null)
{
_resetSet = orderBy(_resetSet).AsQueryable();
}
_resetSet = skip == 0 ? _resetSet.Take(take) : _resetSet.Skip(skip).Take(take);


return _resetSet.AsQueryable();
}

For more detail see the below link http://bulletproofcoder.com/blog/using-include-in-a-generic-entity-framework-repository

In entity framework core we shall use IIncludableQueryable. See the below example code

        public virtual IQueryable<TObject> Filter(Expression<Func<TObject, bool>> filter,
int skip = 0,
int take = int.MaxValue,
Func<IQueryable<TObject>, IOrderedQueryable<TObject>> orderBy = null,
Func<IQueryable<TObject>, IIncludableQueryable<TObject, object>> include = null)
{
var _resetSet = filter != null ? DbSet.AsNoTracking().Where(filter).AsQueryable() : DbSet.AsNoTracking().AsQueryable();


if (include != null)
{
_resetSet = include(_resetSet);
}
if (orderBy != null)
{
_resetSet = orderBy(_resetSet).AsQueryable();
}
_resetSet = skip == 0 ? _resetSet.Take(take) : _resetSet.Skip(skip).Take(take);


return _resetSet.AsQueryable();
}