اعمال توابع تجمعی بر روی چند ستون در Entity framework
نویسنده: وحید نصیری
تاریخ: ۱۳۹۲/۰۸/۰۷ ۸:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
SELECT sum([Rating_TotalRating]), sum([Rating_TotalRaters]), sum([Rating_AverageRating]) FROM [BlogPosts]
context.BlogPost.Select(r =>
new
{
Sum1 = r.Sum(x => x.RatingTotalRating),
Sum2 = r.Sum(x => x.RatingTotalRaters),
Sum3 = r.Sum(x => x.RatingAverageRating)
}).FirstOrDefault(); var sum1 = context.BlogPost.Sum(x => x.RatingTotalRating); var sum2 = context.BlogPost.Sum(x => x.RatingTotalRaters); var sum2 = context.BlogPost.Sum(x => x.RatingAverageRating);
context.BlogPost
.GroupBy(dummyNumber => 0)
.Select(r =>
new
{
Sum1 = r.Sum(x => x.RatingTotalRating),
Sum2 = r.Sum(x => x.RatingTotalRaters),
Sum3 = r.Sum(x => x.RatingAverageRating)
}).FirstOrDefault(); SELECT TOP (1)
[Extent1].[K1] AS [K1],
Sum([Extent1].[A1]) AS [A1],
Sum([Extent1].[A2]) AS [A2],
Sum([Extent1].[A3]) AS [A3]
FROM ( SELECT
0 AS [K1],
[Extent1].[RatingTotalRating] AS [A1],
[Extent1].[RatingTotalRaters] AS [A2],
[Extent1].[RatingAverageRating] AS [A3]
FROM [dbo].[BlogPosts] AS [Extent1]
) AS [Extent1]
GROUP BY [K1] public class Customer
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public CustomerType Type { get; set; }
}
public enum CustomerType
{
Individual,
Institution,
} void ManyQueriesManyCalls()
{
using var scope = serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<CustomerContext>();
var baseQuery = context.Customers.Select(customer => new
{
customer.Name,
customer.Type,
customer.Id,
});
var total = baseQuery.Count();
var types = baseQuery.GroupBy(x => x.Type)
.Select(x => x.Key).ToList();
var pageSize = 10;
var pageIndex = 0;
var results = baseQuery
.OrderBy(x => x.Id)
.Skip(pageSize * pageIndex)
.Take(pageSize)
.ToList();
Console.WriteLine($"Total:{total}, First Type: {types.First()}, First Item: {results.First().Name}");
} SELECT COUNT(*)
FROM [Customers] AS [c]
SELECT [c].[Type]
FROM [Customers] AS [c]
GROUP BY [c].[Type]
SELECT [c].[Name], [c].[Type], [c].[Id]
FROM [Customers] AS [c]
ORDER BY [c].[Id]
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY void ManyQueriesOnCall()
{
using var scope = serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<CustomerContext>();
var baseQuery = context.Customers.Select(customer => new
{
customer.Name,
customer.Type,
customer.Id,
});
var pageSize = 10;
var pageIndex = 0;
var allTogether = baseQuery
.GroupBy(x => 1)
.Select(bq => new
{
Total = baseQuery.Count(),
Types = baseQuery.GroupBy(x => x.Type)
.Select(x => x.Key)
.ToList(),
Results = baseQuery
.OrderBy(x => x.Id)
.Skip(pageSize * pageIndex)
.Take(pageSize)
.ToList(),
})
.FirstOrDefault();
Console.WriteLine($"Total:{allTogether.Total}, First Type: {allTogether.Types.First()}, First Item: {allTogether.Results.First().Name}");
} SELECT [t0].[Key], [t1].[Type], [t2].[Name], [t2].[Type], [t2].[Id]
FROM (
SELECT TOP(1) [t].[Key]
FROM (
SELECT 1 AS [Key]
FROM [Customers] AS [c]
) AS [t]
GROUP BY [t].[Key]
) AS [t0]
OUTER APPLY (
SELECT [c0].[Type]
FROM [Customers] AS [c0]
GROUP BY [c0].[Type]
) AS [t1]
OUTER APPLY (
SELECT [c1].[Name], [c1].[Type], [c1].[Id]
FROM [Customers] AS [c1]
ORDER BY [c1].[Id]
OFFSET @__p_1 ROWS FETCH NEXT @__pageSize_2 ROWS ONLY
) AS [t2]
ORDER BY [t0].[Key], [t1].[Type], [t2].[Id]