我正在尝试从通用存储库更改通用检索方法。但我想改为为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)
我想要例如:
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();
}
最佳答案
我做类似的事情,但使用表达式来急切地加载。也许这会有所帮助:
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();
}