combine multiple queries to one

Viewed 86

currently i'm having troubles with combining two queries into one. I created the solution in two queries as i wasn't able to succeed with only one.

the code i currently have is as follows:

var archivePerson2 = from person in People
                                     join a2 in _context.Archives on (long)person.Id equals a2.ExternalId into archiveId
                                     from archive2 in archiveId.DefaultIfEmpty()
                                     select new PersonWithBarcode
                                     {
                                         Person = person,
                                         Archive = archive2,
                                         Boxes = new List<Box>()
                                     };

                int pageSize = 20;

                SearchResults = People.Count() + (People.Count() == 1 ? " resultaat" : " resultaten");
                NumOfPages = "pagina " + (pageIndex ?? 1) + " van " + (!People.Any() ? 1 : (int)Math.Ceiling(decimal.Divide(People.Count(), pageSize)));
                archivePerson2 = ApplySorting(archivePerson2, sortOrder);
                var personWithBarcodeList = await PaginatedList<PersonWithBarcode>.CreateAsync(archivePerson2.AsTracking(), pageIndex ?? 1, pageSize);
                var listId = personWithBarcodeList.Where(x => x.Archive != null).Select(x => x.Archive.ArchiveId).ToList();
                var archives = from archive in _context.Archives
                                .Include("ArchiveBoxes")        
                                .Include("ArchiveBoxes.Box")
                               where listId.Contains(archive.ArchiveId)
                               select new
                               {
                                   a = archive
                               };
                var kee = archives.ToList();
                foreach (var person in personWithBarcodeList)
                {
                    if (person.Archive != null)
                    {
                        var archive = kee.Where(x => x.a.ArchiveId == person.Archive.ArchiveId).Select(x => x.a).First();
                        person.Archive = archive;
                        person.Boxes = archive.ArchiveBoxes.Select(x => x.Box);
                        if (person.Boxes.Count() > 1)
                        {

                        }
                    }
                }

this results in the following: correct results

this is what i would like to achieve using only one query.

Something i have tried already to get it into one query the include function on the context as following:


                var archivePerson3 = from person in People
                                     join a2 in _context.Archives.Include(x => x.ArchiveBoxes).ThenInclude(x => x.Box) on (long)person.Id equals a2.ExternalId into archiveId
                                     from archive2 in archiveId.DefaultIfEmpty()
                                     select new PersonWithBarcode
                                     {
                                         Person = person,
                                         Archive = archive2,
                                         Boxes = archive2.ArchiveBoxes.Select(x => x.Box).ToList()
                                     };

however this results in the result cutting off after the first item with included boxes: Incorrect results

How am i able to combine these queries into 1 with the correct amount of results?

thanks in advance!

0 Answers
Related