gpt4 book ai didi

c# - 如何选择按月分组的行

转载 作者:行者123 更新时间:2023-11-30 23:02:13 25 4
gpt4 key购买 nike

如何选择按月分组的行。
所以我有一个实体:

public class Security
{
public Guid Id { get; set; }

public string Name { get; set; }

public string Quatation { get; set; }

public SecurityType SecurityType { get; set; }

public double Denomination { get; set; }

public CurrencyType DemoniationType { get; set; }

public virtual ICollection<ReportPeriod> ReportPeriods { get; set; }
}

报告期实体:

 public class ReportPeriod
{
public Guid Id { get; set; }

public DateTime Start { get; set; }

public DateTime End { get; set; }

public Guid SecurityId { get; set; }

public Guid StockExchangeId { get; set; }

public double Amount { get; set; }

public virtual Security Security { get; set; }
}

所以我需要以某种方式获取 ReportPeriod 一年中每个月的总金额。有没有人知道如何去做?

最佳答案

我们可以使用 LINQ 来做到这一点。请在 C# 中找到以下代码片段。希望能帮助到你。在这里,我们按年和月分组,然后合计金额。

namespace Solutions
{
using System;
using System.Collections.Generic;
using System.Linq;

public class Security
{
public Guid Id { get; set; }

public string Name { get; set; }

public string Quatation { get; set; }

//public SecurityType SecurityType { get; set; }

public double Denomination { get; set; }

//public CurrencyType DemoniationType { get; set; }

public virtual ICollection<ReportPeriod> ReportPeriods { get; set; }
}

public class ReportPeriod
{
public Guid Id { get; set; }

public DateTime Start { get; set; }

public DateTime End { get; set; }

public Guid SecurityId { get; set; }

public Guid StockExchangeId { get; set; }

public double Amount { get; set; }

public virtual Security Security { get; set; }
}

public class Entities
{
public static void Main(string[] args)
{

Security security = new Security()
{
Id = Guid.NewGuid(),
Denomination = 1,
Name = "A",
Quatation = "Z",
ReportPeriods = new List<ReportPeriod>()
};
security.ReportPeriods.Add(new ReportPeriod()
{
Amount = 10,
Security = security,
SecurityId = security.Id,
End = DateTime.Now.AddDays(1),
Start = DateTime.Now,
Id = Guid.NewGuid(),
StockExchangeId = Guid.NewGuid()
});
security.ReportPeriods.Add(new ReportPeriod()
{
Amount = 5,
Security = security,
SecurityId = security.Id,
End = DateTime.Now.AddDays(1),
Start = DateTime.Now,
Id = Guid.NewGuid(),
StockExchangeId = Guid.NewGuid()
});
security.ReportPeriods.Add(new ReportPeriod()
{
Amount = 5,
Security = security,
SecurityId = security.Id,
End = DateTime.Now.AddDays(1),
Start = DateTime.Now.AddMonths(-1),
Id = Guid.NewGuid(),
StockExchangeId = Guid.NewGuid()
});

foreach (var groupedReportValues in security.ReportPeriods
.GroupBy(period => new { period.Start.Year, period.Start.Month }).Select(
groupedOnMonth => new
{
StartYear = groupedOnMonth.Key.Year,
StartMonth = groupedOnMonth.Key.Month,
AmountSum = groupedOnMonth.Sum(reportValue => reportValue.Amount)
}))
{
Console.WriteLine(groupedReportValues.StartYear);
Console.WriteLine(groupedReportValues.StartMonth);
Console.WriteLine(groupedReportValues.AmountSum);
Console.WriteLine();
}

Console.ReadLine();
}
}
}

关于c# - 如何选择按月分组的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50566315/

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