[英]LINQ to Entity Query - duplicates in a group
I have the following table that holds a list of shifts and the user assigned to each of those shifts as below: 我有一张下表,其中包含班次列表,以及分配给每个班次的用户,如下所示:
ShiftID | StartDateTime | EndDateTime | AssignedUserID |
-------------------------------------------------------------
What I am trying to do is pull out the shiftID for all shifts that have been assigned to a user on the same date and time. 我想做的是为在同一日期和时间分配给用户的所有班次提取出shiftID。 This should not happen as a person cannot be in two places at once...(this one has been sorted...now) 这不应该发生,因为一个人不能一次位于两个地方...(这个地方已经被分类...现在)
We have some instances in our database where the same user has been assigned to work on 2 different shifts on the same date and time. 我们的数据库中有一些实例,其中已指派同一用户在同一日期和时间进行2个不同的班次。
eg 例如
56 | 06/08/2015 13:00:00 | 06/08/2015 17:00:00 | 22
64 | 06/08/2015 13:00:00 | 06/08/2015 17:00:00 | 22
Hope someone can help with this, even as a start to pull out the users who have been assigned to shifts on the same start date ? 希望有人能帮上忙,即使是开始撤出在同一开始日期被指定轮班的用户?
public IList<ShiftDate> GetDuplicateShifts()
{
return _UoW.ShiftDate.Get()
.ToList();
}
UPDATE UPDATE
OK, I'm getting there, I am now able to pull out all groups of users where the user is on 2 dates on the same day, the code below works: 好的,我到达那里,现在我可以拉出用户在同一天有2个日期的所有用户组,以下代码有效:
public IList<ShiftDate> GetDuplicateShiftsByOrg(int orgID)
{
IList<ShiftDate> AllDates = _UoW.ShiftDates
.Get(s => s.Shift.organisationID == orgID)
.Where(s=>s.assignedUserID != null)
.ToList();
var DuplicateDates = new List<ShiftDate> { };
var groups = AllDates.GroupBy(s=>s.assignedUserID.Value).Where(g => g.Skip(1).Any());
foreach (var group in groups)
{
var group2 = group.GroupBy(sd => sd.shiftStartDate.Date).Where(a => a.Count() > 1).ToList();
if (group2.Count() > 1)
{
///// REF 1 : this pulls out all shiftdates in group one, I want all shiftdates in group2.
foreach (ShiftDate shiftDate in group)
{
DuplicateDates.Add(shiftDate);
}
/////////////////////////////////////////////////////////
}
}
return DuplicateDates.ToList();
}
As you will see at REF one above in the code I want to pull out the shiftdates in group2. 正如您将在上面的REF中看到的那样,我想提取出group2中的shiftdates。 However when I try to do this I get the following error: 但是,当我尝试执行此操作时,出现以下错误:
Unable to cast object of type 'Grouping[System.DateTime,MySolution.POCO.ShiftDate]' to type 'MySolution.POCO.ShiftDate'. 无法将类型为“ Grouping [System.DateTime,MySolution.POCO.ShiftDate]”的对象转换为类型为“ MySolution.POCO.ShiftDate”的对象。
UPDATE UPDATE
The error above was appearing because I was trying to return a shiftdate from a group of shift dates, I had to add an extra loop to cycle through the groups first. 出现上述错误是因为我试图从一组轮班日期中返回一个轮班日期,所以我必须添加一个额外的循环才能首先在各个组之间循环。 See solution below 请参阅下面的解决方案
try this: 尝试这个:
var dt=DateTime.Now().ToString("MM/dd/yyyy");
var result = from t in Table1
WHERE c.StartDate=dt
SELECT new {c.AssignedUserID};
Finally got this working by using multiple groups, see working code below: 最后通过使用多个组使此工作正常,请参见下面的工作代码:
public IList<ShiftDate> GetDuplicateShiftsByOrg(int orgID)
{
IList<ShiftDate> AllDates = _UoW.ShiftDates
.Get(s => s.Shift.organisationID == orgID)
.Where(s=>s.assignedUserID != null)
.ToList();
var DuplicateDates = new List<ShiftDate> { };
/// 1. Group all shifts assigned to user
var groupUserShifts = AllDates.GroupBy(s=>s.assignedLocumID.Value).Where(g => g.Skip(1).Any());
foreach (var group in groupUserShifts)
{
/// 2. Group all shifts assigned to user on the same date
var groupUserSameDates = group.GroupBy(sd => sd.shiftStartDate.Date).Where(a => a.Count() > 1).ToList();
if (groupUserSameDates.Count() > 1)
{
foreach (var dategroup in groupUserSameDates)
{
/// 3. Return all shifts where user hass been assigned on the same date
foreach (ShiftDate shiftDate in dategroup)
{
DuplicateDates.Add(shiftDate);
}
}
}
}
return DuplicateDates.ToList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.