gpt4 book ai didi

c# - 将 DateTime 条目列表转换为自定义摘要列表

转载 作者:行者123 更新时间:2023-11-30 18:32:51 25 4
gpt4 key购买 nike

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

}

}

最佳答案

好的,谢谢橡皮鸭。毕竟能够使用循环,只是必须大声思考......

注意具有以下代码块的部分:

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())
{
}
}
}

关于c# - 将 DateTime 条目列表转换为自定义摘要列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18232373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com