ASP.Net Core MVC - Razor使用efcore显示每个月/年类别的费用总计行和每月总费用。
创始人
2024-09-15 06:01:52
0

要实现显示每个月/年类别的费用总计行和每月总费用,您可以按照以下步骤进行操作:

  1. 创建一个ASP.Net Core MVC项目。

  2. 安装所需的NuGet包:

    • Microsoft.EntityFrameworkCore
    • Microsoft.EntityFrameworkCore.SqlServer
  3. 创建一个表示费用的实体类:

public class Expense
{
    public int Id { get; set; }
    public string Category { get; set; }
    public decimal Amount { get; set; }
    public DateTime Date { get; set; }
}
  1. 创建一个表示数据库上下文的类:
public class ExpenseContext : DbContext
{
    public ExpenseContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet Expenses { get; set; }
}
  1. 在Startup.cs文件中添加数据库上下文的配置:
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddControllersWithViews();
}
  1. 创建一个控制器类,用于处理费用相关的操作:
public class ExpenseController : Controller
{
    private readonly ExpenseContext _context;

    public ExpenseController(ExpenseContext context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        var monthlyTotal = _context.Expenses
            .GroupBy(e => new { e.Category, e.Date.Year, e.Date.Month })
            .Select(g => new
            {
                Category = g.Key.Category,
                Year = g.Key.Year,
                Month = g.Key.Month,
                Total = g.Sum(e => e.Amount)
            })
            .OrderBy(g => g.Year)
            .ThenBy(g => g.Month)
            .ToList();

        var yearlyTotal = _context.Expenses
            .GroupBy(e => new { e.Category, e.Date.Year })
            .Select(g => new
            {
                Category = g.Key.Category,
                Year = g.Key.Year,
                Total = g.Sum(e => e.Amount)
            })
            .OrderBy(g => g.Year)
            .ToList();

        ViewBag.MonthlyTotal = monthlyTotal;
        ViewBag.YearlyTotal = yearlyTotal;

        return View();
    }
}
  1. 创建一个Razor视图来显示费用总计行和每月总费用:
@model IEnumerable

Monthly Total

@foreach (var item in Model) { }
Category Year Month Total
@item.Category @item.Year @item.Month @item.Total

Yearly Total

@foreach (var item in ViewBag.YearlyTotal) { }
Category Year Total
@item.Category @item.Year @item.Total

以上代码将从数据库中检索费用数据,并使用efcore执行分组和求和操作,然后将结果传递给Razor视图进行显示。在视图中,您可以使用循环来遍历结果并在表格中显示数据。

相关内容

热门资讯

开挂辅助软件"fis... 您好:fishpoker透视底牌这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
必看攻略“wepoker私人局... 必看攻略“wepoker私人局辅助挂”开挂辅助神器(透视)安装教程是一款可以让一直输的玩家,快速成为...
开挂辅助工具"德州透... 开挂辅助工具"德州透视竞技联盟"开挂(透视)辅助软件(真实有挂);无需打开直接搜索薇:1367043...
玩家爆料“微扑克辅助器官网入口... 无需打开直接搜索;操作使用教程:玩家爆料“微扑克辅助器官网入口”开挂辅助工具(透视)存在挂教程1、界...
开挂辅助工具"hhp... >>您好:hhpoker到底可以辅助吗确实是有挂的,很多玩家在这款hhpoker到底可以辅助吗游戏中...
记者揭秘“wepoker有透视... >>您好:wepoker有透视软件挂吗确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
开挂辅助插件"wpk... wpk是真的还是假的 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由:...
信息共享“wepoker有没有... wepoker有没有辅助挂是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户...
开挂辅助下载"hhp... 开挂辅助下载"hhpoker脚本下载"开挂(透视)辅助平台(真是有挂);无需打开直接搜索打开薇:13...
实测发现“wepoker辅助透... >>>您好:,软件加微信【添加136704302】确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...