使用Linq,有没有办法将这两行合并到一个方法链中?
Group group = await _database.Groups.AsQueryable()
.Include(g => g.Children)
.Include(g => g.Documents)
.SingleOrDefaultAsync(g => g.Pk == id);
group.Children = @group.Children.Where(c => c.Active).AsEnumerable().ToList();
编辑以解释结构:
group
的类型为Group
Children
上的Group
属性为List<Group>
答案 0 :(得分:1)
您可以让数据库为您进行过滤,而不是带回所有子记录:
var group = await _database.Groups.AsQueryable()
.Where(g => g.Pk == id)
.Select(g => new {
Children = g.Children.Where(c => c.Active).ToList(),
Documents = g.Documents.ToList() } )
.SingleOrDefaultAsync();