private static Dictionary<string, Substance> BuildSubstanceDictionaryAndUpdateDbIfNeeded(List<LcboProduct> lcboProducts) { Dictionary<string, Substance> knownSubstancesDictionary = new Dictionary<string, Substance>(); IEnumerable<string> allLcboSubstanceNames = from p in lcboProducts select p.Name; IQueryable<string> knownSubstanceNames = from s in unitOfWork.Substances.FindAll() select s.Name; List<string> knownSubstanceNamesList = new List<string>(); // Force the query to evaluate and put the results in a list foreach (string substanceName in knownSubstanceNames) { knownSubstanceNamesList.Add(substanceName); } Console.WriteLine(String.Format("There are {0} substances in the DB", knownSubstanceNames.Count())); IEnumerable<string> newSubstanceNames = from s in allLcboSubstanceNames.Distinct() where !knownSubstanceNamesList.Contains(s) select s; int newSubstanceCount = 0; foreach (var substance in newSubstanceNames) { Substance newSubstance = new Substance() { Name = substance }; unitOfWork.Substances.Add(newSubstance); newSubstanceCount++; } Console.WriteLine(String.Format("There are {0} new substances on the LCBO website", newSubstanceCount)); unitOfWork.Commit(); Console.WriteLine("Updated DB"); foreach (Substance substance in unitOfWork.Substances.FindAll()) { knownSubstancesDictionary.Add(substance.Name, substance); } Console.WriteLine(String.Format("Built dictionary of {0} substances", knownSubstancesDictionary.Count)); return knownSubstancesDictionary; }
private Dictionary<string, Substance> BuildSubstanceDictionaryAndUpdateDbIfNeeded2(List<LcboProduct> lcboProducts, StringBuilder result, IConnection signalrConnection) { Dictionary<string, Substance> knownSubstancesDictionary = new Dictionary<string, Substance>(); IEnumerable<string> allLcboSubstanceNames = from p in lcboProducts select p.Name; IQueryable<string> knownSubstanceNames = from s in domainContext.Substances select s.Name; List<string> knownSubstanceNamesList = new List<string>(); // Force the query to evaluate and put the results in a list foreach (string substanceName in knownSubstanceNames) { knownSubstanceNamesList.Add(substanceName); } signalrConnection.Broadcast(String.Format("There are {0} substances in the DB", knownSubstanceNames.Count())); result.AppendFormat("There are {0} substances in the DB\r\n", knownSubstanceNames.Count()); IEnumerable<string> newSubstanceNames = from s in allLcboSubstanceNames.Distinct() where !knownSubstanceNamesList.Contains(s) select s; int newSubstanceCount = 0; foreach (var substance in newSubstanceNames) { Substance newSubstance = new Substance() { Name = substance }; domainContext.Save(newSubstance); newSubstanceCount++; } signalrConnection.Broadcast(String.Format("There are {0} new substances on the LCBO website\r\n", newSubstanceCount)); result.AppendFormat("There are {0} new substances on the LCBO website\r\n", newSubstanceCount); domainContext.SaveChanges(); signalrConnection.Broadcast("Updated DB"); result.Append("Updated DB\r\n"); foreach (Substance substance in domainContext.Substances) { knownSubstancesDictionary.Add(substance.Name, substance); } signalrConnection.Broadcast(String.Format("Built dictionary of {0} substances", knownSubstancesDictionary.Count)); result.AppendFormat("Built dictionary of {0} substances\r\n", knownSubstancesDictionary.Count); return knownSubstancesDictionary; }