将多个查询合二为一

时间:2021-02-05 13:47:20

标签: c# entity-framework asp.net-core-3.1

目前我在将两个查询合并为一个时遇到了麻烦。 我在两个查询中创建了解决方案,因为我无法仅通过一个查询成功。

我目前拥有的代码如下:

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)
                        {

                        }
                    }
                }

结果如下: correct results

这是我想只使用一个查询来实现的。

我已经尝试将其放入一个查询上下文中的包含函数中,如下所示:


                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()
                                     };

然而,这会导致结果在第一个包含框的项目之后被切断: Incorrect results

我如何能够将这些查询合并为 1 个具有正确结果数量的查询?

提前致谢!

0 个答案:

没有答案