EntityFramework Generic Repository, multiple include?

asp.net, c#, linq

Solution

I do something similar, but instead use expressions to eagerly load. Maybe this will help:

    public TEntity Item(Expression<Func<TEntity, bool>> wherePredicate, params Expression<Func<TEntity, object>>[] includeProperties)
    {
        foreach (var property in includeProperties)
        {
            _dbSet.Include(property);
        }
        return _dbSet.Where(wherePredicate).FirstOrDefault();
    }

Problem

I am trying to change my generic retrieve method from my generic repository. But I want instead to pass a string for the includeproperties, to pass this: `params Expression<Func<TEntity, object>>[] includeProperties = null` The thing is when I call this method: ``` public virtual IEnumerable<TEntity> Retrieve(Expression<Func<TEntity, bool>> filter = null, params Expression<Func<TEntity, object>>[] includeProperties = null) ``` I want for example: `TEntityExample.Retrieve(filter: c=>c.Id=Id, includeProperties:c=> c.propertynav1, e=> e.propertynav1.propertynav3, e=> e.Prop4)` Or just if dont need navigation properties `TEntityExample.Retrieve(filter: c=>c.Id=Id)` But dont know why the `includeProperties:` is not working, is not accepted, anyone know why, or if I am doing something wrong. I want the possibility to dont pass the includeProperties or to pass it by specifying the `includeProperties:` ``` public virtual IEnumerable<TEntity> Retrieve(Expression<Func<TEntity, bool>> filter = null, string includeProperties = "") { IQueryable<TEntity> query = _dbSet; if (filter != null) { query = query.Where(filter); } if (!string.IsNullOrEmpty(includeProperties)) { foreach (var includeProperty in includeProperties.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } } return query.ToList(); } ```

Original source