diff --git a/Source/DataLayer/QueryObjects/LibraryBookQueries.cs b/Source/DataLayer/QueryObjects/LibraryBookQueries.cs index b18506e3..2dbd0ddc 100644 --- a/Source/DataLayer/QueryObjects/LibraryBookQueries.cs +++ b/Source/DataLayer/QueryObjects/LibraryBookQueries.cs @@ -43,20 +43,16 @@ namespace DataLayer .Include(le => le.Book).ThenInclude(b => b.ContributorsLink).ThenInclude(c => c.Contributor) .Include(le => le.Book).ThenInclude(b => b.Category).ThenInclude(c => c.ParentCategory); - public static IEnumerable FindOrphanedEpisodes(this IEnumerable libraryBooks) - { - var parentedEpisodes = - libraryBooks - .Where(lb => lb.Book.IsEpisodeParent()) - .SelectMany(s => libraryBooks.FindChildren(s)); + public static IEnumerable ParentedEpisodes(this IEnumerable libraryBooks) + => libraryBooks.Where(lb => lb.Book.IsEpisodeParent()).SelectMany(s => libraryBooks.FindChildren(s)); - return - libraryBooks + public static IEnumerable FindOrphanedEpisodes(this IEnumerable libraryBooks) + => libraryBooks .Where(lb => lb.Book.IsEpisodeChild()) .ExceptBy( - parentedEpisodes + libraryBooks + .ParentedEpisodes() .Select(ge => ge.Book.AudibleProductId), ge => ge.Book.AudibleProductId); - } #nullable enable public static LibraryBook? FindSeriesParent(this IEnumerable libraryBooks, LibraryBook seriesEpisode) diff --git a/Source/LibationWinForms/GridView/ProductsGrid.cs b/Source/LibationWinForms/GridView/ProductsGrid.cs index eacf9a8e..578a83b8 100644 --- a/Source/LibationWinForms/GridView/ProductsGrid.cs +++ b/Source/LibationWinForms/GridView/ProductsGrid.cs @@ -156,6 +156,7 @@ namespace LibationWinForms.GridView var allEntries = bindingList.AllItems().BookEntries(); var seriesEntries = bindingList.AllItems().SeriesEntries().ToList(); + var parentedEpisodes = dbBooks.ParentedEpisodes(); foreach (var libraryBook in dbBooks.OrderBy(e => e.DateAdded)) { @@ -163,7 +164,8 @@ namespace LibationWinForms.GridView if (libraryBook.Book.IsProduct()) AddOrUpdateBook(libraryBook, existingEntry); - else if(libraryBook.Book.IsEpisodeChild()) + else if(parentedEpisodes.Any(lb => lb == libraryBook)) + //Only try to add or update is this LibraryBook is a know child of a parent AddOrUpdateEpisode(libraryBook, existingEntry, seriesEntries, dbBooks); }