我为我的实体创建了一个存储库Master
。在存储库中,我有一个Get
使用 Entity Core 通过 Id 获取实体的方法。
该方法接收:
public TEntity Get(object id, params Expression<Func<TEntity, object>>[] includedRelatedEntities)
{
return GetById(IncludeEntities(DBContext.Set<TEntity>().AsQueryable(), includedRelatedEntities), id);
}
然后,当我在代码中使用它时,我只需将我正在查找的实体的 id 以及我需要包含在查询中的相关实体的表达式树传递给该方法(Expression<Func<TEntity, object>>
)
使用示例如下:
var master = MasterRepository.Get(1, x => x.BranchOffice.Location);
在这种情况下,我正在寻找 Id = 1 的 Master,并且我希望它包括BranchOffice
相关实体及Location
与此相关BranchOffice
.
从一个到多个关系,它工作得很好,但是对于相关列表,我不知道如何使用表达式来解决它。
例如,如果我想包括Product
列表的实体Detail
named Details
与我有关Master
,不知道如何在表达式树中表达。
var master = MasterRepository.Get(1, x => x.Details.Product);
详细信息是一个列表,因此我无法像上面的示例中那样访问产品。
我该如何表达这一点Expression<Func<TEntity, object>>
?
EDIT:
我已经尝试过:
var master = MasterRepository.Get(1, x => x.Details.Select(y=> y.Product));
但我遇到以下异常:
属性表达式 'x => {from Detail y in [x].Details select
[y].Product}' 无效。表达式应该代表一个属性
访问:'t => t.MyProperty'。有关包含相关内容的更多信息
数据,请参阅 go.microsoft.com/fwlink/?LinkID=746393。