gpt4 book ai didi

c# - 计算平均值时是否可以使用 Skip and Take?

转载 作者:行者123 更新时间:2023-11-30 17:34:36 26 4
gpt4 key购买 nike

我正在尝试使用 LINQ to Entities 创建查询,以计算产品的平均评分。在排序评级之前,我将每个产品的平均值转换到一个匿名对象。然后我只想获取一部分结果。

查询如下:

var query = Items.GroupBy(review => review.Product)
.Select(group => new
{
Product = group.Key,
Rating = group.Average(review => review.Rating)
})
.OrderByDescending(pr => pr.Rating)
.Skip(itemsToSkip)
.Take(count)
.ToList();

生成的查询如下所示:

{SELECT 
1 AS [C1],
[GroupBy1].[K1] AS [ProductId],
[GroupBy1].[K2] AS [TotalImages],
[GroupBy1].[K3] AS [TotalVideos],
[GroupBy1].[K4] AS [TotalAudioRecordings],
[GroupBy1].[K5] AS [Name],
[GroupBy1].[K6] AS [DiscountedPrice],
[GroupBy1].[K7] AS [Status],
[GroupBy1].[A1] AS [C2]
FROM ( SELECT
[Extent2].[ProductId] AS [K1],
[Extent2].[TotalImages] AS [K2],
[Extent2].[TotalVideos] AS [K3],
[Extent2].[TotalAudioRecordings] AS [K4],
[Extent2].[Name] AS [K5],
[Extent2].[DiscountedPrice] AS [K6],
[Extent2].[Status] AS [K7],
AVG([Extent1].[Rating]) AS [A1]
FROM [dbo].[ProductReviews] AS [Extent1]
LEFT OUTER JOIN [dbo].[Products] AS [Extent2] ON [Extent1].[Product_ProductId] = [Extent2].[ProductId]
GROUP BY [Extent2].[ProductId], [Extent2].[TotalImages], [Extent2].[TotalVideos], [Extent2].[TotalAudioRecordings], [Extent2].[Name], [Extent2].[DiscountedPrice], [Extent2].[Status]
) AS [GroupBy1]}

查询无法跳过任何项目,或只获取一些结果。相反,它会返回数据库中每个产品的平均值计算结果。

是否可以像我尝试的那样使用SkipTake?如果是这样,我如何更正查询以获得所需的结果?我最好不要从数据库中拉回所有数据。

最佳答案

我认为您可能需要在查询中再添加一个选择:

var query = Items.GroupBy(review => review.Product)
.Select(group => new
{
Product = group.Key,
Rating = group.Average(review => review.Rating)
})
.OrderByDescending(pr => pr.Rating)
.Select(x => new { Product = x.Product, Rating = x.Rating })
.Skip(itemsToSkip)
.Take(count)
.ToList();

关于c# - 计算平均值时是否可以使用 Skip and Take?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42185116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com