C# 检查db中的值列表并检索属于日期-时间-月份属性的项

C# 检查db中的值列表并检索属于日期-时间-月份属性的项,c#,linq,asp.net-core,C#,Linq,Asp.net Core,拜托,伙计们, 我想要实现的是返回前6个月,搜索我的事务表,并检索属于日期时间和金额的每个月属性的事务列表 例如。当前日期为2020年3月4日最后6个月的日期变为02/20,01/2020,12/2019,11/2019,10/2019,09/2019现在我想搜索一个表transactions,该表具有DateTime属性DateCreated,检索每个月内发生的所有交易,并对所有交易进行汇总数量 ResponseCode 00表示成功付款 我已经试过了 List<DateTime&g

拜托,伙计们, 我想要实现的是返回前6个月,搜索我的事务表,并检索属于日期时间和金额的每个月属性的事务列表

例如。当前日期为2020年3月4日最后6个月的日期变为
02/20,01/2020,12/2019,11/2019,10/2019,09/2019
现在我想搜索一个表
transactions
,该表具有
DateTime
属性
DateCreated
,检索每个月
内发生的所有交易
,并对所有交易进行汇总数量

ResponseCode 00表示成功付款
我已经试过了


 List<DateTime> months = new List<DateTime>();
    List<double> MonthSum= new List<Double>();        
            DateTime[] lastSixMonths = Enumerable.Range(0, 6).Select(i => DateTime.Now.AddMonths(-i)).ToArray();

foreach (var month in lastSixMonths)
{
    var monthString = month.ToString("MM/yyyy");
            var trans = await _context.Transactions.Where(c => monthString.Contains(c.DateCreated.ToString()) && c.ResponseCode == "00").Select(c => c.Amount).ToListAsync();
            var sum = trans.Sum();
            MonthSum.Add(sum);

}


列表月份=新列表();
List MonthSum=新列表();
DateTime[]lastSixMonths=Enumerable.Range(0,6)。选择(i=>DateTime.Now.AddMonths(-i)).ToArray();
foreach(最近六个月的var月)
{
var monthString=月份ToString(“MM/yyyy”);
var trans=wait_context.Transactions.Where(c=>monthString.Contains(c.DateCreated.ToString())和&c.ResponseCode==“00”)。选择(c=>c.Amount.toListSync();
var sum=trans.sum();
月数加(总和);
}

我这样做似乎有点不对劲。请帮忙

我希望这就是你需要的:

var fromDate = DateTime.Now.AddMonths(-6);
var sumByMonth = _context.Transactions.Where(d => d.CreateDate > fromDate)
                    .GroupBy(d => d.CreateDate.Month)
                    .Select(d => new { Month = d.Key, Sum = d.Sum(docs => docs.Amount) })
                    .ToDictionary(a => a.Month , b => b.Sum);

DateTime字段在linq中显示为“yyyy MM dd”,因此您需要将月份更改为“yyyy MM”进行判断

在where条件中,需要交换“Contains”条件

        List<DateTime> months = new List<DateTime>();
        List<double> MonthSum = new List<Double>();
        DateTime[] lastSixMonths = Enumerable.Range(0, 6).Select(i => DateTime.Now.AddMonths(-i)).ToArray();
        foreach (var month in lastSixMonths)
        {
            var monthString = month.ToString("yyyy-MM");
            var trans = await _context.Transactions.Where(c => c.DateCreated.ToString().Contains(monthString) && c.ResponseCode == "00").Select(c => c.Amount).ToListAsync();
            var sum = trans.Sum();
            MonthSum.Add(sum);

        }
列表月份=新列表();
List MonthSum=新列表();
DateTime[]lastSixMonths=Enumerable.Range(0,6)。选择(i=>DateTime.Now.AddMonths(-i)).ToArray();
foreach(最近六个月的var月)
{
var monthString=月份ToString(“yyyy-MM”);
var trans=wait_context.Transactions.Where(c=>c.DateCreated.ToString().Contains(monthString)和&c.ResponseCode==“00”)。选择(c=>c.Amount.toListSync();
var sum=trans.sum();
月数加(总和);
}

回答得好。基于这个问题,我认为OP希望
fromDate
是本月的第一个,尽管如此,该月的所有交易都会返回。例:6个月前是9/4,但9/1-现在返回交易。很好,但起始日期应该是过去6个月的日期列表。它需要按年份添加组,这样就可以了