3

Punches = new List<TimeClock>(); // Populated from database ...

既存のリスト:

[Employee] - [DateTime] - [Action]
x1 - y1 - in
x2 - y2 - in
x1 - y3 - out
x3 - y4 - in
x1 - y5 - in
x3 - y6 - out
x2 - y7 - out
x1 - y8 - out
x2 - y9 - in
x2 - y10 - out

必要なリスト形式:

[Employee] - [Start] - [End] - [Hours]
x1 - y1 - y3 - z1
x1 - y5 - y8 - z2
x2 - y2 - y7 - z3
x2 - y9 - y10 - z4
x3 - y4 - y6 - z5

私はList、従業員/時間/アクションを取り、それを従業員/開始/終了/期間の要約リストにフラット化し、各従業員が対応する出入り時間ごとに1行にしようとしています。

新しいリストを作成する方法がわかりません。または、それをList? 私は配列を使用して従業員の要約を作成する PHP から来ていますが、C# でこれを行う方法がわかりません。

以下のコードは、メインのリストから従業員ごとの勤務時間の要約を取得する方法です。要約リストを作成するために同様の構造/ループを使用できると思いましたが、正確な方法はわかりません。またstruct、リストを定義するために使用しようとしました...誰かが私を正しい方向に向けてください!

    /// <summary>
    /// Populates HoursWorked List
    /// </summary>
    public void PopulateHoursWorkedList()
    {
        // Get list of time clock punches
        PayPeriodPunches = new List<TimeClock>();

        if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            using (var db = new Database())
            {
                // Load with Employee
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<TimeClock>(tc => tc.Employee);
                db.LoadOptions = dlo;

                PayPeriodPunches = db.TimeClockPunches
                    .Where(cp => cp.ClockPunchDateTime >= PayPeriodSelected.BeginDateTime
                                && cp.ClockPunchDateTime <= PayPeriodSelected.EndDateTime)
                    .OrderBy(cp => cp.ClockPunchDateTime)
                    .ToList();
            }

        // Get unique list of employees and sum of hours worked
        HoursWorkedList = new List<EmployeeHoursWorked>();
        foreach (TimeClock tcp in PayPeriodPunches)
        {
            if (!HoursWorkedList.Exists(e => e.Employee == tcp.Employee))
            {
                EmployeeHoursWorked ehw = new EmployeeHoursWorked();
                ehw.Employee = tcp.Employee;
                HoursWorkedList.Add(ehw);
            }
        }

        foreach (EmployeeHoursWorked ehw in HoursWorkedList.ToList())
        {
            Employee e = ehw.Employee;
            // This employees punches
            List<TimeClock> thisEmpPunches = PayPeriodPunches
                .Where(pp => pp.Employee == e)
                .OrderBy(pp => pp.ClockPunchDateTime)
                .ToList();

            bool hasClockedIn = false;
            bool hasClockedOut = false;
            int i = 0;
            DateTime dts = new DateTime();
            DateTime dte = new DateTime();
            TimeSpan dur = new TimeSpan();

            foreach (TimeClock tcp in thisEmpPunches)
            {
                if (tcp.ClockAction == ClockAction.In)
                {
                    dts = tcp.ClockPunchDateTime;
                    hasClockedIn = true;
                    i = 1;
                }

                if (tcp.ClockAction == ClockAction.Out)
                {
                    dte = tcp.ClockPunchDateTime;

                    // Was clocked In: Use previous clock-in time
                    if (i == 1)
                    {
                        dur = dte - dts;
                    }
                    else
                    {
                        // Not clocked in, never clocked in, assume clocked in since beginning of PP
                        if (!hasClockedIn)
                        {
                            dts = PayPeriodSelected.BeginDateTime;
                            dur = dte - dts;
                        }
                    }

                    // Update employee hours worked duration
                    int index = HoursWorkedList.FindIndex(z => z.Employee == e);
                    EmployeeHoursWorked tmp = HoursWorkedList[index];
                    tmp.HoursWorked += dur;
                    HoursWorkedList[index] = tmp;

                    hasClockedOut = true;
                    i = 2;
                }

            }

            if (i == 1)
            {
                // Employee never clocked out, assume end of PP (or now if sooner)
                if (DateTime.Now < PayPeriodSelected.EndDateTime)
                {
                    dte = DateTime.Now;
                }
                else
                {
                    dte = PayPeriodSelected.EndDateTime;
                }

                // Update employee hours worked duration
                dur = dte - dts;
                int index = HoursWorkedList.FindIndex(z => z.Employee == e);
                EmployeeHoursWorked tmp = HoursWorkedList[index];
                tmp.HoursWorked += dur;
                HoursWorkedList[index] = tmp;
            }

        }

    }
4

2 に答える 2

0

わかりました、ありがとうラバーダッキー。結局、ループを使用することができました。大声で考えなければなりませんでした...

次のコード ブロックの部分に注意してください。

EmployeeClockSummary ecs = new EmployeeClockSummary();
ecs.Employee = e;
ecs.ClockInTime = dts;
ecs.ClockOutTime = dte;
HoursSummaryList.Add(ecs);

作品:

/// <summary>
/// Populates HoursSummary List
/// </summary>
public void PopulateHoursSummaryList()
{
    HoursSummaryList = new List<EmployeeClockSummary>();

    // Get list of time clock punches
    PayPeriodPunches = new List<TimeClock>();

    if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject()))
    using (var db = new Database())
    {
        // Load with Employee
        DataLoadOptions dlo = new DataLoadOptions();
        dlo.LoadWith<TimeClock>(tc => tc.Employee);
        db.LoadOptions = dlo;

        PayPeriodPunches = db.TimeClockPunches
                .Where(cp => cp.ClockPunchDateTime >= PayPeriodSelected.BeginDateTime
                            && cp.ClockPunchDateTime <= PayPeriodSelected.EndDateTime)
                .OrderBy(cp => cp.ClockPunchDateTime)
                .ToList();
    }

    // Get unique list of employees and sum of hours worked
    HoursWorkedList = new List<EmployeeHoursWorked>();
    foreach (TimeClock tcp in PayPeriodPunches)
    {
        if (!HoursWorkedList.Exists(e => e.Employee == tcp.Employee))
        {
            EmployeeHoursWorked ehw = new EmployeeHoursWorked();
            ehw.Employee = tcp.Employee;
            HoursWorkedList.Add(ehw);
        }
    }

    foreach (EmployeeHoursWorked ehw in HoursWorkedList.ToList())
    {
        Employee e = ehw.Employee;
        // This employees punches
        List<TimeClock> thisEmpPunches = PayPeriodPunches
            .Where(pp => pp.Employee == e)
            .OrderBy(pp => pp.ClockPunchDateTime)
            .ToList();

        bool hasClockedIn = false;
        bool hasClockedOut = false;
        int i = 0;
        DateTime dts = new DateTime();
        DateTime dte = new DateTime();
        TimeSpan dur = new TimeSpan();

        foreach (TimeClock tcp in thisEmpPunches)
        {
            if (tcp.ClockAction == ClockAction.In)
            {
                dts = tcp.ClockPunchDateTime;
                hasClockedIn = true;
                i = 1;
            }

            if (tcp.ClockAction == ClockAction.Out)
            {
                dte = tcp.ClockPunchDateTime;

                // Was clocked In: Use previous clock-in time
                if (i == 1)
                {
                    dur = dte - dts;

                    EmployeeClockSummary ecs = new EmployeeClockSummary();
                    ecs.Employee = e;
                    ecs.ClockInTime = dts;
                    ecs.ClockOutTime = dte;
                    HoursSummaryList.Add(ecs);
                }
                else
                {
                    // Not clocked in, never clocked in, assume clocked in since beginning of PP
                    if (!hasClockedIn)
                    {
                        dts = PayPeriodSelected.BeginDateTime;
                        dur = dte - dts;

                        EmployeeClockSummary ecs = new EmployeeClockSummary();
                        ecs.Employee = e;
                        ecs.ClockInTime = dts;
                        ecs.ClockOutTime = dte;
                        HoursSummaryList.Add(ecs);
                    }
                }

                // Update employee hours worked duration
                int index = HoursWorkedList.FindIndex(z => z.Employee == e);
                EmployeeHoursWorked tmp = HoursWorkedList[index];
                tmp.HoursWorked += dur;
                HoursWorkedList[index] = tmp;

                hasClockedOut = true;
                i = 2;
            }
        }

        if (i == 1)
        {
            // Employee never clocked out, assume end of PP (or now if sooner)
            if (DateTime.Now < PayPeriodSelected.EndDateTime)
            {
                dte = DateTime.Now;
            }
            else
            {
                dte = PayPeriodSelected.EndDateTime;
            }

            EmployeeClockSummary ecs = new EmployeeClockSummary();
            ecs.Employee = e;
            ecs.ClockInTime = dts;
            ecs.ClockOutTime = dte;
            HoursSummaryList.Add(ecs);

            // Update employee hours worked duration
            dur = dte - dts;
            int index = HoursWorkedList.FindIndex(z => z.Employee == e);
            EmployeeHoursWorked tmp = HoursWorkedList[index];
            tmp.HoursWorked += dur;
            HoursWorkedList[index] = tmp;
        }

        if (HoursSummaryList.Any())
        {
        }
    }
}
于 2013-08-14T13:21:24.823 に答える