C# 如何使用日期过滤和EF进行求和?

C# 如何使用日期过滤和EF进行求和?,c#,entity-framework-core,C#,Entity Framework Core,在我的系统中,我有一个包含参与者体重历史记录(参与者数据)的表格: 我需要获得权重的总和,但参与者必须只计算一次特定日期之前或之前的最新权重 以下是我目前掌握的情况: DateTime dt = DateTime.Now; DateTime? chartStartDate = new DateTime(Event.Event.StartDateTime.Value.Year, Event.Event.StartDateTime.Value.Month, Event.Event.StartDate

在我的系统中,我有一个包含参与者体重历史记录(参与者数据)的表格:

我需要获得权重的总和,但参与者必须只计算一次特定日期之前或之前的最新权重

以下是我目前掌握的情况:

DateTime dt = DateTime.Now;
DateTime? chartStartDate = new DateTime(Event.Event.StartDateTime.Value.Year, Event.Event.StartDateTime.Value.Month, Event.Event.StartDateTime.Value.Day);
DateTime? chartEndDate = dt < Event.Event.EndDateTime ? dt : Event.Event.EndDateTime;
bool chartLoop = true;

if (chartStartDate < dt) {
    // Get all weights
    var participantsWeightStats = await _context.ParticipantData.ToListAsync();
    // Loop date with steps of 7
    do {
        // Get the sum of `participantsWeightStats` before or equal to the loop-date
        // In the following I need to Distinct/GroupBy ParticipantId so every participant only counts with the latest weight 
        var chartData = participantsWeightStats.Where(x => x.Date <= chartStartDate).OrderByDescending(x => x.Date).ToList();
        ViewData["PData_Values"] += chartData.Sum(x => x.Weight).ToString().Replace(".", "").Replace(",", ".") + ",";
        ViewData["PData_Labels"] += "'" + chartStartDate.Value.ToString("d") + "',";
        chartStartDate = chartStartDate.Value.AddDays(7);
        if (chartStartDate > chartEndDate && chartLoop) {
            chartStartDate = chartEndDate;
            chartLoop = false;
        }
    } while (chartStartDate <= chartEndDate);
}
DateTime dt=DateTime.Now;
约会时间?chartStartDate=新日期时间(Event.Event.StartDateTime.Value.Year、Event.Event.StartDateTime.Value.Month、Event.Event.StartDateTime.Value.Day);
约会时间?chartEndDate=dtx.Date x.Date).ToList();
ViewData[“PData_Values”]+=chartData.Sum(x=>x.Weight).ToString().Replace(“.”,”).Replace(“,”,”)+“,”;
ViewData[“PData_标签”]+=“””+chartStartDate.Value.ToString(“d”)+“,”;
chartStartDate=chartStartDate.Value.AddDays(7);
如果(chartStartDate>chartEndDate&&chartLoop){
chartStartDate=chartEndDate;
chartLoop=false;
}
}同时(chartStartDate尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ParticipantId", typeof(int));
            dt.Columns.Add("Weight", typeof(int));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Rows.Add(new object[] {1, 86, DateTime.Parse("2020-01-30")});
            dt.Rows.Add(new object[] {1, 83, DateTime.Parse("2020-02-03")});
            dt.Rows.Add(new object[] {2, 98, DateTime.Parse("2020-01-20")});
            dt.Rows.Add(new object[] {2, 96, DateTime.Parse("2020-01-26")});
            dt.Rows.Add(new object[] {2, 75, DateTime.Parse("2020-02-06")});

            DateTime endDate = DateTime.Parse("2020-01-31");

            int sum = dt.AsEnumerable()
                .Where(x => x.Field<DateTime>("Date") <= endDate)
                .OrderByDescending(x => x.Field<DateTime>("Date"))
                .GroupBy(x => x.Field<int>("ParticipantId"))
                .Sum(x => x.First().Field<int>("Weight"));


        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
DataTable dt=新的DataTable();
添加(“ParticipantId”,typeof(int));
添加(“重量”,类型(int));
添加(“日期”,类型(日期时间));
Add(新对象[]{1,86,DateTime.Parse(“2020-01-30”)});
Add(新对象[]{1,83,DateTime.Parse(“2020-02-03”)});
Add(新对象[]{2,98,DateTime.Parse(“2020-01-20”)});
Add(新对象[]{2,96,DateTime.Parse(“2020-01-26”)});
Add(新对象[]{2,75,DateTime.Parse(“2020-02-06”)});
DateTime endDate=DateTime.Parse(“2020-01-31”);
int sum=dt.AsEnumerable()
其中(x=>x.Field(“日期”)x.Field(“日期”))
.GroupBy(x=>x.Field(“ParticipantId”))
.Sum(x=>x.First().字段(“权重”);
}
}
}

谢谢,@jdweng.
第一个()
在总和中我无法计算。当我尝试分组时,我无法得到权重。但你能从我的问题中看出,如果我做错了什么吗?我相信我得到了错误的结果。每7天给出的权重总和大致相同,我知道这不是事实。这可能与时区有关。结束日期是午夜。因此如果时间是在与结束日期不同的时区中采集的,因此错误地切断了日期。假设您在PST,数据是在EST中采集的。而EST中凌晨1:00采集的数据是在PST 10:00。然后根据结束数据,此示例可能包含或不包含。我明白了,但我认为情况并非如此。我从另一个网站上知道r计算,总重量损失约为3000公斤。当我做循环时,第一次总重量约为95000公斤(参与者第一天的总重量)。这可能是对的。但是下面的7天步骤大约是110000,这不是真的。经过一些测试,我可以看到,查询是对循环日期之前或等于循环日期的所有a参与者权重求和-它应该只得到最新的。你的日期有问题。我发布的代码得到的和是86+96=182。如果你只要一周,你就需要一个开始日期和结束日期。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ParticipantId", typeof(int));
            dt.Columns.Add("Weight", typeof(int));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Rows.Add(new object[] {1, 86, DateTime.Parse("2020-01-30")});
            dt.Rows.Add(new object[] {1, 83, DateTime.Parse("2020-02-03")});
            dt.Rows.Add(new object[] {2, 98, DateTime.Parse("2020-01-20")});
            dt.Rows.Add(new object[] {2, 96, DateTime.Parse("2020-01-26")});
            dt.Rows.Add(new object[] {2, 75, DateTime.Parse("2020-02-06")});

            DateTime endDate = DateTime.Parse("2020-01-31");

            int sum = dt.AsEnumerable()
                .Where(x => x.Field<DateTime>("Date") <= endDate)
                .OrderByDescending(x => x.Field<DateTime>("Date"))
                .GroupBy(x => x.Field<int>("ParticipantId"))
                .Sum(x => x.First().Field<int>("Weight"));


        }
    }
}