private async Task ProcessIncludes(List <IEntity> results, CancellationToken ct) { if (!Includes.Any()) { return; } //TODO: optimize / parallel processing foreach (var propertyDefinition in Includes) { var targetEntityDefinition = (IEntityDefinition)propertyDefinition.PropertyType.GetTargetValueType(propertyDefinition); var foreignIds = results .Select(x => x[propertyDefinition.Name]) .Where(x => x != null) .Cast <IEntity>() .Select(x => _repository.GetDocumentId(targetEntityDefinition, x.Id)) .Distinct(); var foreignQuery = new DocumentDbQuery(_repository, targetEntityDefinition) .Add(Criterion.In(MetaConstants.IdProperty, foreignIds.Cast <object>().ToArray())); var foreignEntities = (await foreignQuery.ToEnumerable <IEntity>(ct)).ToDictionary(x => x.Id); results.ForEach(x => { var foreignId = ((IEntity)x[propertyDefinition.Name])?.Id; if (foreignId != null && foreignEntities.ContainsKey(foreignId)) { x[propertyDefinition.Name] = foreignEntities[foreignId]; } }); } }
private static void AddCriterion(IQuery query, string selector, StringValues selectorValues) { if (selectorValues.Count > 1) { query.Add(Criterion.In(selector, selectorValues)); } else { query.Add(Criterion.Eq(selector, selectorValues.FirstOrDefault())); } }