EntityFramework Generic Repository,包括多个?

use*_*557 2 c# linq asp.net

我试图从我的通用存储库更改我的通用检索方法.但是我想要为includeproperties传递一个字符串来传递这个:params Expression<Func<TEntity, object>>[] includeProperties = null 事情就是当我调用这个方法时:

public virtual IEnumerable<TEntity> Retrieve(Expression<Func<TEntity, bool>> filter = null, params Expression<Func<TEntity, object>>[] includeProperties = null)
Run Code Online (Sandbox Code Playgroud)

我想要例如: TEntityExample.Retrieve(filter: c=>c.Id=Id, includeProperties:c=> c.propertynav1, e=> e.propertynav1.propertynav3, e=> e.Prop4)

或者只是不需要导航属性 TEntityExample.Retrieve(filter: c=>c.Id=Id)

但不知道为什么includeProperties:不工作,不被接受,任何人都知道为什么,或者我做错了什么.我希望有可能不通过includeProperties或通过指定传递它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();
            }
Run Code Online (Sandbox Code Playgroud)

MOR*_*ARD 10

我做了类似的事情,而是使用表达式来急切加载.也许这会有所帮助:

    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();
    }
Run Code Online (Sandbox Code Playgroud)