/**** * Saves the gainers in database. ****/ public void PopulateGainers() { IEXHandler webHandler = new IEXHandler(); List <StockStats> gainersListRes = webHandler.Gainers(); String gainersData = JsonConvert.SerializeObject(gainersListRes); List <StockStats> gainersList = null; if (gainersData != "") { var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore }; gainersList = JsonConvert.DeserializeObject <List <StockStats> >(gainersData, settings); } foreach (StockStats gainer in gainersList) { //Database will give PK constraint violation error when trying to insert record with existing PK. //So add company only if it doesnt exist, check existence using symbol (PK) if (dbContext.Gainers.Where(c => c.symbol.Equals(gainer.symbol)).Count() == 0) { Gainers gainerData = new Gainers(gainer.symbol, gainer.companyName, gainer.primaryExchange, gainer.sector); dbContext.Gainers.Add(gainerData); } } dbContext.SaveChanges(); }
private void GetGainersCallback(IEnumerable <Stock> gainers) { Gainers.Clear(); foreach (Stock stock in gainers) { Gainers.Add(stock); } }