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

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/

10-10 11:19