我遇到了优化LINQ query
的问题。在初始加载时,我的xhr请求几乎是8MB大,从服务器获取这些数据需要一段时间。我有Auctions
列表,其中只显示了几列(您可以在UserAuctionListDTO
中看到)。
我上传了图片,其中显示了DTOs
如何连接以便更好地理解。 CurrentsToUserAccount
中的收藏集仅获得Id
和Name
我的查询目标是选择分配给auctions
或UserContact
或Organization
或Region
的所有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);
}
我想知道我是否可以改进它。