using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using API.Entities.Progress;
using Microsoft.EntityFrameworkCore;
namespace API.Extensions.QueryExtensions;
public class IdCount
{
public int Id { get; set; }
public int Count { get; set; }
}
///
/// Extensions primarily focusing on Profile and Server Stats pages/queries
///
public static class StatisticsQueryExtensions
{
public static async Task> GetTopCounts( this IQueryable query, Expression> keySelector, int? take = null)
{
var result = query
.GroupBy(keySelector)
.Select(g => new IdCount {Id = g.Key, Count = g.Count()})
.OrderByDescending(x => x.Count);
if (take.HasValue)
{
return await result.Take(take.Value).ToListAsync();
}
return await result.ToListAsync();
}
}