LINQ查询传输海量数据

时间:2018-01-24 17:38:58

标签: c# entity-framework linq optimization query-optimization

我遇到了优化LINQ query的问题。在初始加载时,我的xhr请求几乎是8MB大,从服务器获取这些数据需要一段时间。我有Auctions列表,其中只显示了几列(您可以在UserAuctionListDTO中看到)。

我上传了图片,其中显示了DTOs如何连接以便更好地理解。 CurrentsToUserAccount中的收藏集仅获得IdName

enter image description here

我的查询目标是选择分配给auctionsUserContactOrganizationRegion的所有Department。拍卖至少有一个Price时有效。

这是我的查询。它可能有点乱,但它向我展示了转移数据的大小容量的最佳结果。 Collection of Prices中的每个UserAuctionListDTO获得了大约100-1000条记录,平均大约为600条。

protected override IQueryable<UserAuctionListDTO> GetQueryable()
    {
        return AllAuctionToUser
            .Where(a => a.GarancyFromDate <= _dateTimeProvider.Now
                        && a.GarancyToDate >= _dateTimeProvider.Now
                        && !a.IsBlocked
                        && a.IsVisibleOnFe
                        && a.Prices.Any(price => price.PriceMJ > 0
                                                 && (price.Current.UserContacts.Any(x =>
                                                         x.Id == Filter.UserContactId)
                                                     || price.Current.Departments.Any(x =>
                                                         x.Id == Filter.DepartmentId)
                                                     || price.Current.Organizations.Any(x =>
                                                         x.Id == Filter.OrganizationId)
                                                     || price.Current.Regions.Any(x => x.Id == Filter.RegionId))))
            .Select(auction => new UserAuctionListDTO
            {
                Id = auction.Id,
                Code = auction.Code,
                Name = auction.Name,
                ProductCount = auction.Prices.Count,
                AttachmentsCount = auction.Attachments.Count,
                Category = new CategoryBaseDTO
                {
                    Name = auction.Category.Name
                },
                GarancyFromDate = auction.GarancyFromDate,
                GarancyToDate = auction.GarancyToDate,
                Prices = auction.Prices
                    .Select(x => new PriceToUserAuctionDTO
                    {
                        Id = x.Id,
                        Current = new CurrentsToUserAccount
                        {
                            Id = x.Current.Id,
                            UserContacts = x.Current.UserContacts.Where(u => u.IsBlocked == false)
                                .Select(z => new UserContactBaseDTO
                                {
                                    Id = Filter.UserContactId
                                }

                                ),
                            Departments = x.Current.Departments.Where(u => u.IsBlocked == false)
                                .Select(z => new DepartmentBaseDTO
                                {
                                    Id = Filter.DepartmentId
                                }),
                            Regions = x.Current.Regions.Where(u => u.IsBlocked == false)
                                .Select(z => new RegionBaseDTO
                                {
                                    Id = Filter.RegionId
                                }),
                            Organizations = x.Current.Organizations.Where(u => u.IsBlocked == false)
                                .Select(z => new OrganizationBaseDTO
                                {
                                    Id = Filter.OrganizationId
                                }),
                        },
                        SpecificProductSuppliers = new SpecificProductSuplierToUserAccountDTO
                        {
                            Id = x.SpecificProductSuppliers.Id,
                            CatalogName = x.SpecificProductSuppliers.CatalogName,

                            SpecificProduct = new SpecificProductToUserAccountDTO
                            {
                                Id = x.SpecificProductSuppliers.SpecificProduct.Id,
                                Name = x.SpecificProductSuppliers.SpecificProduct.Name
                            }
                        }

                    })
            }).OrderBy(x => x.Code);
    }

从服务器传输了多少数据的屏幕截图 enter image description here

我想知道我是否可以改进它。

0 个答案:

没有答案