private void UpdateEntity(IChallengeResultCollection <IChallengeResult> entity) { using (var ctx = new HappyDogShowContext()) { foreach (IChallengeResult result in entity.Results) { var foundResults = ctx.BreedChallengeResults.Where(i => i.ID == result.Id); if (foundResults.Count() == 1) { BreedChallengeResult foundResult = foundResults.First(); foundResult.EntryNumber = result.EntryNumber; } } ctx.SaveChanges(); } }
private void PopulateBreedChallengeResultsTableForDogShow(int dogShowId) { using (var ctx = new HappyDogShowContext()) { var breedsthathaveentries = from e in ctx.BreedEntries where e.Show.ID == dogShowId select e.Dog.Breed; breedsthathaveentries = breedsthathaveentries.Distinct(); var whatwemusthaveeventually = from bc in ctx.BreedChallenges from b in breedsthathaveentries from s in ctx.DogShows.Where(i => i.ID == dogShowId) select new { BreedChallenge = bc, Breed = b, Show = s }; var whatwehavenow = from res in ctx.BreedChallengeResults where res.DogShow.ID == dogShowId select res; var whatwehavewithwhatwemusthave = from i in whatwemusthaveeventually join res in whatwehavenow on new { breedid = i.Breed.ID, breedchallengeid = i.BreedChallenge.ID, dogshowid = i.Show.ID } equals new { breedid = res.Breed.ID, breedchallengeid = res.BreedChallenge.ID, dogshowid = res.DogShow.ID } into gj from joinedresult in gj.DefaultIfEmpty() select new { breedid = i.Breed.ID, breed = i.Breed, breedchallengeid = i.BreedChallenge.ID, breedchallenge = i.BreedChallenge, dogshowid = i.Show.ID, dogshow = i.Show, existingdata = joinedresult }; var yes = whatwehavewithwhatwemusthave.ToList(); var whatwemustcreate = yes.Where(i => i.existingdata == null); foreach (var tocreate in whatwemustcreate) { BreedChallengeResult realEntry = new BreedChallengeResult() { DogShow = tocreate.dogshow, Breed = tocreate.breed, BreedChallenge = tocreate.breedchallenge, EntryNumber = "" }; ctx.BreedChallengeResults.Add(realEntry); } ctx.SaveChanges(); } }