C# 将日期时间项列表转换为自定义摘要列表
C# 将日期时间项列表转换为自定义摘要列表,c#,linq-to-sql,C#,Linq To Sql,punchs=newlist();//从数据库填充… 现有名单: [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
punchs=newlist();//从数据库填充…
现有名单:
[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
我正在尝试获取员工/时间/行动的列表
,并将其扁平化为员工/开始/结束/持续时间的摘要列表,每个员工对应的入/出时间有一行
我不知道如何创建新列表,或者我是否应该称之为列表
?我来自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;
}
}
}
//
///填充工作小时列表
///
public void PopulateHoursWorkedList()
{
//获取时钟打孔列表
PayPeriodPunchs=新列表();
如果(!System.ComponentModel.DesignerProperties.GetIsInDesignMode(新的DependencyObject()))
使用(var db=new Database())
{
//满载员工
DataLoadOptions dlo=新的DataLoadOptions();
dlo.LoadWith(tc=>tc.Employee);
db.LoadOptions=dlo;
PayPeriodPunchs=db.TimeClockPunchs
.其中(cp=>cp.ClockPunchDateTime>=PayPeriodSelected.BeginDateTime
&&cp.ClockPunchDateTime(cp.ClockPunchDateTime)
.ToList();
}
//获取唯一的员工列表和工作时间总和
HoursWorkedList=新列表();
foreach(时钟tcp,以支付周期为单位)
{
如果(!HoursWorkedList.Exists(e=>e.Employee==tcp.Employee))
{
EmployeeHoursWorked ehw=新的EmployeeHoursWorked();
ehw.Employee=tcp.Employee;
工时列表。添加(ehw);
}
}
foreach(EmployeeHoursWorkedList.ToList()中的员工工作小时数ehw)
{
员工e=ehw.员工;
//这是员工打拳
列出此empPunchs=PayPeriodPunchs
.其中(pp=>pp.Employee==e)
.OrderBy(pp=>pp.ClockPunchDateTime)
.ToList();
bool hasClockedIn=false;
bool hasClockedOut=false;
int i=0;
DateTime dts=新的DateTime();
DateTime dte=新的DateTime();
TimeSpan dur=新的TimeSpan();
foreach(此EMPS中的时钟tcp)
{
if(tcp.ClockAction==ClockAction.In)
{
dts=tcp.ClockPunchDateTime;
hasClockedIn=真;
i=1;
}
if(tcp.ClockAction==ClockAction.Out)
{
dte=tcp.ClockPunchDateTime;
//已打卡:使用上一个打卡时间
如果(i==1)
{
dur=dte-dts;
}
其他的
{
//未打卡,从未打卡,假设从PP开始打卡
如果(!hascolledin)
{
dts=PayPeriodSelected.BeginDateTime;
dur=dte-dts;
}
}
//更新员工工作时间
int index=HoursWorkedList.FindIndex(z=>z.Employee==e);
EmployeeHoursWorked tmp=HoursWorkedList[索引];
tmp.工作小时数+=dur;
小时工作列表[索引]=tmp;
hasClockedOut=真;
i=2;
}
}
如果(i==1)
{
//员工从未打卡下班,假设PP结束(或者现在,如果更早)
if(DateTime.Nowz.Employee==e);
EmployeeHoursWorked tmp=HoursWorkedList[索引];
tmp.工作小时数+=dur;
小时工作列表[索引]=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())
{
}
}
}
//
///填充小时摘要列表
///
public void PopulateHoursSummaryList()
{
HoursSummaryList=新列表();
//获取时钟打孔列表
PayPeriodPunchs=新列表();
如果(!System.ComponentModel.DesignerProperties.GetIsInDesignMode(新的DependencyObject()))
使用(var db=new Database())
{
//满载员工
DataLoadOptions dlo=新的DataLoadOptions();
dlo.LoadWith(tc=>tc.Employee);
db.LoadOptions=dlo;
PayPeriodPunchs=db.TimeClockPunchs
.其中(cp=>cp.ClockPunchDateTime>=PayPeriodSelected.BeginDateTime
&&cp.ClockPunchDateTime(cp.ClockPunchDateTime)
.ToList();
}
//获取唯一的员工列表和工作时间总和
HoursWorkedList=新列表();
foreach(时钟tcp,以支付周期为单位)
{
如果(!HoursWorkedList.Exists(e=>e.Employee==tcp.Employee))
{
员工工作小时数ehw=新
foreach (var empInOuts in PayPeriodPunches
.OrderBy(x => x.ClockPunchDateTime)
.GroupBy(x => x.Employee))
/// <summary>
/// Populates HoursDuration List
/// </summary>
public void PopulateHoursDurationList()
{
HoursDurationList = new ObservableCollection<EmployeeClockSummary>();
try
{
// Punches grouped by Employee
foreach (var empInOuts in PayPeriodPunches
.OrderBy(x => x.ClockPunchDateTime)
.GroupBy(x => x.Employee))
{
int empCount = 0;
foreach (var empPunch in empInOuts)
{
if (empPunch.ClockAction == ClockAction.In)
{
// Clock In
HoursDurationList.Add(new EmployeeClockSummary
{
Employee = empPunch.Employee,
ClockInTime = empPunch.ClockPunchDateTime
});
}
else
{
// Clock Out
if (empCount == 0)
{
// This emp first punch is clock out (emp was clock in on previous pp)
HoursDurationList.Add(new EmployeeClockSummary
{
Employee = empPunch.Employee,
ClockOutTime = empPunch.ClockPunchDateTime
});
}
else
{
// Add clockout to previous clock in entry
var last = HoursDurationList[HoursDurationList.Count - 1];
last.ClockOutTime = empPunch.ClockPunchDateTime;
HoursDurationList[HoursDurationList.Count - 1] = last;
}
}
empCount++;
}
}
}
catch (Exception e)
{
}
// Check for any missing ClockOuts ie still clocked in, ClockIns ie was clocked in at start
ObservableCollection<EmployeeClockSummary> tempList = new ObservableCollection<EmployeeClockSummary>();
foreach (var x in HoursDurationList)
{
var change = x;
if (change.ClockOutTime == DateTime.MinValue
|| change.ClockOutTime == null)
{
// Set clock out date to now (or pp end if now passed it)
// as they are still clocked in
if (DateTime.Now > PayPeriodSelected.EndDateTime)
{
change.ClockOutTime = PayPeriodSelected.EndDateTime;
}
else
{
change.ClockOutTime = DateTime.Now;
}
}
// Set clock in date to beginning of PP
// as they were already clocked in when it started
if (change.ClockInTime == DateTime.MinValue
|| change.ClockInTime == null)
{
change.ClockInTime = PayPeriodSelected.BeginDateTime;
}
tempList.Add(change);
}
// Group by Employee
HoursDurationList = tempList;
HoursDurationListView = new ListCollectionView(HoursDurationList);
HoursDurationListView.GroupDescriptions.Add(new PropertyGroupDescription("Employee"));
}