public static async Task <IList <T> > HandleListAsync <T>(BaseSinglePkRepository <T> repository, IList <Guid> idCollection) where T : class, ISinglePkDataAccessObject, new() { var resultList = new List <T>(); var existingEntities = await repository.GetListAsync(entity => idCollection.Any(id => id == entity.Id)).ConfigureAwait(false); resultList.AddRange(existingEntities); var diff = idCollection.Where(id => existingEntities.All(entity => entity.Id != id)).ToList(); if (diff.Count == 0) { return(resultList); } var newEntities = diff.Select(id => new T { Id = id }).ToList(); var createdEntities = await repository.CreateAsync(newEntities).ConfigureAwait(false); resultList.AddRange(createdEntities); return(resultList); }
public static async Task <IList <T> > HandleListAsync <T>(BaseSinglePkRepository <T> repository, IList <Guid> idCollection, Func <T, bool> prepareFunc = null, IList <string> navigationProperties = null) where T : class, ISinglePkDataAccessObject, new() { var resultList = new List <T>(); var existingEntities = await repository.GetListAsync( entity => idCollection.Any(id => id == entity.Id), navigationProperties).ConfigureAwait(false); resultList.AddRange(existingEntities); var diff = idCollection.Where(id => existingEntities.All(entity => entity.Id != id)).ToList(); if (diff.Count == 0) { return(resultList); } var newEntities = diff.Select(id => new T { Id = id }).ToList(); if (prepareFunc != null) { var finalList = new List <T>(); foreach (var entity in newEntities) { if (prepareFunc(entity)) { finalList.Add(entity); } } newEntities = finalList; } var createdEntities = await repository.CreateAsync(newEntities).ConfigureAwait(false); resultList.AddRange(createdEntities); return(resultList); }