/// <summary> /// Stores the show list. /// </summary> /// <param name="list">The list.</param> /// <param name="getCastOfShow">A function to get the cast of one show.</param> /// <returns> /// A Task. /// </returns> public async Task StoreShowList(List <ShowDto> list, Func <int, Task <List <CastMemberDto> > > getCastOfShow) { var mongoinsertlist = new List <ShowWithCast>(list.Count); foreach (var orgshow in list) { var filter = Builders <ShowWithCast> .Filter.Eq(s => s.Id, orgshow.Id); var storedShow = await this.collection.Find(filter).FirstOrDefaultAsync().ConfigureAwait(false); if (storedShow == null) { var show = new ShowWithCast { Id = orgshow.Id, Name = orgshow.Name, ImdbId = orgshow.ImdbId, ImdbRating = orgshow.ImdbRating, }; await ReplaceCast(orgshow, show).ConfigureAwait(false); mongoinsertlist.Add(show); } else { storedShow.Name = orgshow.Name; storedShow.ImdbId = orgshow.ImdbId; storedShow.ImdbRating = orgshow.ImdbRating; await ReplaceCast(orgshow, storedShow).ConfigureAwait(false); await this.collection.ReplaceOneAsync(filter, storedShow).ConfigureAwait(false); } } // then store the shows including their cast if (mongoinsertlist.Any()) { await this.collection.InsertManyAsync(mongoinsertlist).ConfigureAwait(false); } async Task ReplaceCast(ShowDto orgshow, ShowWithCast show) { if (!(orgshow.CastMembers is null) && orgshow.CastMembers.Any()) { show.Cast.Clear(); show.Cast.AddRange(orgshow.CastMembers); }
private List <ShowWithCast> ShowsWithCasts(PagingParameterModel pagingparametermodel) { List <ShowWithCast> results = new List <ShowWithCast>(); try { _ShowCastcontext = new ShowCastContext(); _Showcontext = new ShowContext(); _Castcontext = new CastContext(); var shows = from sh in _Showcontext.Show select sh; int ii = shows.Count(); var showsList = shows.Skip((pagingparametermodel.pageNumber - 1) * pagingparametermodel.pageSize).Take(pagingparametermodel.pageSize).ToList(); foreach (Show s in showsList) { ShowWithCast sc = new ShowWithCast(); sc.ShowID = s.ShowID; sc.Name = s.ShowName; var showPairs = from sp in _ShowCastcontext.ShowCast where sp.ShowID == s.ShowID select sp; sc.Cast = new List <Cast>(); foreach (ShowCast item in showPairs) { var casts = from cst in _Castcontext.Cast where cst.CastID == item.CastID select cst; foreach (Cast aCast in casts) { sc.Cast.Add(aCast); } } sc.Cast = sc.Cast.OrderByDescending(x => x.BirthDay).ToList(); results.Add(sc); } } catch (Exception ex) { string er = ex.ToString(); } return(results); }