Как получить сумму с фильтрацией даты и отличить от EF? - PullRequest
0 голосов
/ 10 февраля 2020

В моей системе у меня есть таблица с историей веса участников (ParticipantData):

ParticipantId  Weight    Date
1              86        2020-01-30
1              83        2020-02-03
2              98        2020-01-20
2              96        2020-01-26
3              75        2020-02-06

Мне нужно получить сумму весов, но участник должен только посчитать один раз с последним весом до или равный указанному c date.

Вот что у меня есть:

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);
}

Я пробовал разные подходы с GroupBy и Distinct, но безуспешно.

1 Ответ

1 голос
/ 10 февраля 2020

Попробуйте следующее:

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"));


        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...