gpt4 book ai didi

c# - EntityFramework 通用存储库,多个包含?

转载 作者:行者123 更新时间:2023-11-30 19:17:31 25 4
gpt4 key购买 nike

我正在尝试从我的通用存储库更改我的通用检索方法。但我想为 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();
}

关于c# - EntityFramework 通用存储库,多个包含?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17821468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com