private void FillTournaments() { Tournaments.Clear(); if (flag1) { lock (_itemsLock) { flag1 = false; SortableObservableCollection <IMatchVw> matches = new SortableObservableCollection <IMatchVw>(); int tournamentsAmount = ChangeTracker.IsLandscapeMode ? 10 : 8; SyncObservableCollection <SportCategory> tempCategories = new SyncObservableCollection <SportCategory>(); matches = Repository.FindMatches(matches, "", SelectedLanguage, MatchFilter, Sort); //lets see if we can start from categories, not tournaments var groups = matches.Where(x => !x.IsOutright && x.CategoryView != null).Select(x => x.CategoryView).Distinct().ToList(); foreach (var group in groups) { SportCategory temp = new SportCategory(group.DisplayName, new SyncObservableCollection <TournamentVw>(), group.LineObject.GroupId); temp.Sort = group.LineObject.Sort.Value; tempCategories.Add(temp); } tempCategories = new SyncObservableCollection <SportCategory>(tempCategories.OrderBy(x => x.Sort).ToList()); foreach (SportCategory category in tempCategories) { //fill tournaments - not outrights List <TournamentVw> Tournaments = new List <TournamentVw>(); var tours = matches.Where(x => !x.IsOutright && x.TournamentView != null && x.CategoryView != null && x.CategoryView.LineObject.GroupId == category.CategoryID).Select(x => x.TournamentView).Distinct().ToList(); int allTournamentsCount = tours.Count; for (int i = 0; i < tours.Count; i++) { long id = tours[i].LineObject.GroupId; long svrId = tours[i].LineObject.SvrGroupId; string tournamentName = tours[i].DisplayName; long countryId = 0; string country = ""; int sort = tours[i].LineObject.Sort.Value; string minCombination = null; if (tours[i].LineObject.GroupTournament != null && tours[i].LineObject.GroupTournament.MinCombination.Value > 0) { minCombination = TranslationProvider.Translate(MultistringTags.TERMINAL_X_COMB, tours[i].LineObject.GroupTournament.MinCombination.Value); } if (tours[i].TournamentCountryView != null) { countryId = tours[i].TournamentCountryView.LineObject.SvrGroupId; country = tours[i].TournamentCountryView.DisplayName; } long sportId = 0; string sportName = ""; if (tours[i].TournamentSportView != null) { sportId = tours[i].TournamentSportView.LineObject.SvrGroupId; sportName = tours[i].TournamentSportView.DisplayName; } bool isOutright = false; long categoryId = 0; TournamentVw tour = new TournamentVw(id, svrId, tournamentName, countryId, sort, minCombination, country, sportId, isOutright, sportName, categoryId); tour.TemporaryMatchesCount = matches.Where(x => !x.IsOutright && x.TournamentView != null && x.TournamentView.LineObject.GroupId == tour.Id).Count(); tour.ApplayTemporaryMatchesCount(); if (ChangeTracker.SelectedTournaments.Contains(tour.Id.ToString() + "*0")) { tour.IsSelected = true; } Tournaments.Add(tour); } Tournaments = new List <TournamentVw>(Tournaments.OrderBy(x => x.Sort)); Tournaments.Sort(Comparison); //now i have all category tournaments for (int i = 0; i < tournamentsAmount; i++) { if (i >= Tournaments.Count) { break; } category.Tournaments.Add(Tournaments[i]); } Tournaments.Clear(); //if there is only outrights, add them to category int outrightCount = matches.Where(x => x.IsOutright && x.CategoryView != null && x.CategoryView.LineObject.GroupId == category.CategoryID).Count(); if (outrightCount > 0) { allTournamentsCount++; } if (category.Tournaments.Count == 0 || category.Tournaments.Count < tournamentsAmount) { if (outrightCount > 0) { TournamentVw outright = new TournamentVw(int.MinValue, 0, TranslationProvider.Translate(MultistringTags.OUTRIGHTS).ToString(), 0, int.MinValue, null, "", category.CategoryID, true, category.SportName); outright.MatchesCount = outrightCount; category.Tournaments.Add(outright); } } //add all tournaments button if (allTournamentsCount > category.Tournaments.Count) { category.Tournaments.RemoveAt(category.Tournaments.Count - 1); TournamentVw allTournaments = new TournamentVw(int.MinValue, -999, "All tournaments", 0, int.MinValue, null, "", category.CategoryID, true, category.SportName); allTournaments.MatchesCount = allTournamentsCount; category.Tournaments.Add(allTournaments); } } SetImageCategories(tempCategories); Dispatcher.BeginInvoke((Action)(() => { Categories.ApplyChanges(tempCategories); })); } } }