How to group monthly sales by currency in Blazor Server application?

Cenk 946 Reputation points
2024-01-22T17:57:58.0733333+00:00

Hello, I am working on a Blazor Server application and I want to get the monthly total sales. I need to group these sales by the currency (USD, EURO, TL) that they were sold. Here is the query I am currently using to group monthly sales, but I need to group by currency as well. How can I group by monthly sales based on currency?

public async Task<ICollection<ReportSalesDto>> GetMonthlySales(int year, string status)
{
    await using var ctx = await _db.CreateDbContextAsync();

    return await ctx.OrdersDetail
        .Where(x => x.Order.OrderDateTime.Year == year && x.Status == status)
        .GroupBy(x => new { Month = x.Order.OrderDateTime.Month, Status = x.Status, Currency = x.Currency })
        .Select(u => new ReportSalesDto()
        {
            Month = u.Key.Month,
            //Status = u.Key.Status,
            //Currency = u.Key.Currency,
            TotalSales = u.Sum(x => x.TotalSellPrice)
        })
        .OrderBy(u => u.Month)
        .ToListAsync();
}
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
679 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,991 questions
{count} votes