/// <summary> /// Imports the list of currently available stocks. /// </summary> /// <returns>A task that will complete when the import has completed.</returns> public async Task ImportStocks() { // Collect both list of stocks: stored and currently available. var storedStocks = (await _entityQuerySource.Query(q => q.Get <Stock>())).ToDictionary(x => x.Isin); var currentStocks = await _stockQueryService.QueryAll(); await _entityRepositoryFactory.Use(async repository => { foreach (var stock in currentStocks) { // Create stock in storage if not yet present. if (!storedStocks.TryGetValue(stock.Isin, out Stock matchingStoredStock)) { repository.Add(stock); } // Update name if this has changed. else if (stock.Name != matchingStoredStock.Name) { await repository.Change <Stock>(matchingStoredStock.Id, s => { s.Name = stock.Name; }); } } // For now, do not delete obsolete stocks... }); }