gpt4 book ai didi

c# - 在时间范围内对获取项目进行分组

转载 作者:行者123 更新时间:2023-11-30 17:32:33 26 4
gpt4 key购买 nike

我有这个 EntityFramework DatabaseContext 给定:

class Worker
{
int Id,
string Name,
...
ICollection<Shift> Shifts
}

class Shift
{
int Id,
DateTime ShiftStart
DateTime ShiftEnd
...
Worker Worker
}

class CustomerAction
{
int Id,
Worker Worker,
DateTime ArrivalTime,
int ActionType
...
string Comment
}

现在我想将所有 Worker 及其轮类分组,然后获取 ActionType 为 2 或 4 的所有 CustomerActions。

有时 worker 不添加他们的类次,那么 worker 完成的所有其他 CustomerActions 应该列出空类次信息。

输出应该是这样的:

List = 
{
new Item
{
WorkerId = 1, WorkerName = "Worker1" ...
ShiftId = 1, ShiftStart = 10/1/2017 1:00:00 AM, ShiftEnd = 10/1/2017 5:00:00 AM ...

// Sorted by ArrivalTime, only in between the ShiftStart / ShiftEnd Range, done by this Worker
CustomerActions =
{
new CustomerAction { ActionId = 1, ActionType = 4, ArrivalTime = 10/1/2017 1:00:00 AM, Comment = "My comment" }
new CustomerAction { ActionId = 2, ActionType = 2, ArrivalTime = 10/1/2017 1:30:00 AM, Comment = "Some other comment" }
new CustomerAction { ActionId = 3, ActionType = 4, ArrivalTime = 10/1/2017 2:00:00 AM, Comment = "No comment" }
}
}
new Item
{
WorkerId = 2, WorkerName = "Worker2" ...
ShiftId = null, ShiftStart = null, ShiftEnd = null ...

// Sorted by ArrivalTime, done by this Worker, Without an Shift
CustomerActions =
{
new CustomerAction { ActionId = 4, ActionType = 4, ArrivalTime = 10/2/2017 1:00:00 AM, Comment = "..." }
new CustomerAction { ActionId = 5, ActionType = 2, ArrivalTime = 10/3/2017 1:30:00 AM, Comment = "..." }
new CustomerAction { ActionId = 6, ActionType = 4, ArrivalTime = 10/4/2017 2:00:00 AM, Comment = "..." }
}
}
}

最佳答案

如果我对您的问题的理解正确,则您不需要分组。相反,您需要扩展,因为每个 worker 都会转移所处理的操作。下面的代码应该为您指明正确的方向:

var registeredShifts = dbContext.Workers
.SelectMany(w => w.Shifts.Select(s => new
{
WorkerId = w.Id,
WorkerName = w.Name,
ShiftId = s.Id,
ShiftStart = s.ShiftStart,
ShiftEnd = s.ShiftEnd,
CustomerActions = dbContext.CustomerActions
.Where(a => a.Worker.Id == w.Id &&
a.ArrivalTime >= s.ShiftStart &&
a.ArrivalTime <= s.ShiftEnd &&
(a.ActionType == 2 || a.ActionType == 4))
.ToList()
}))

编辑:要在已注册类次之外的操作获得相同的结果,您必须使用分组。

var outsideShifts = dbContrxt.CustomerActions
.Where(a => a.ActionType == 2 || a.ActionType == 4)
.Where(a => a.Worker.Shifts.All(s => a.ArrivalTime < s.ShiftStart ||
a.ArrivalTime > s.ShiftEnd))
.GroupBy(a => new
{
WorkerId = a.Worker.Id,
WorkerName = a.Worker.Name
})
.Select(g => new
{
WorkerId = g.Key.WorkerId,
WorkerName = g.Key.WorkerName,
ShiftId = null,
ShiftStart = null,
ShiftEnd = null,
CustomerActions = g.ToList()
});

最后得到需要的数据,Union()上面的结果:

var result = registeredShifts.Union(outsideShifts);
return result.ToArray();

关于c# - 在时间范围内对获取项目进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46490682/

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