[英]Avoiding record duplication in LINQ
我有一个记录被复制的情况,但我不知道如何处理。 这是LINQ语句:
theData = (from urls in this.ObjectContext.Activities.AsExpandable()
.Where(predicateA)
.Where(r => (r.StartDate >= beginDate && r.StartDate <= endDate) ||
(r.EndDate >= beginDate && r.EndDate <= endDate) ||
(r.StartDate <= beginDate && r.EndDate >= endDate))
join idGroups in this.ObjectContext.IdentityGroups
on urls.IdentityID equals idGroups.IdentityID
join groupSup in this.ObjectContext.GroupSupervisors
.Where(r => r.SupervisorID == loggedInID)
on idGroups.GroupID equals groupSup.GroupID
join programs in progs
on urls.ProcessName.ToUpper() equals programs.ProcessName.ToUpper()
into jt
from jt1 in jt.DefaultIfEmpty()
.Where(r => r == null || r.Ignore == false)
group urls by new
{ urls.ProcessName,
urls.ContextID,
jt1.CustomCategory,
jt1.Name,
groupSup.SupervisorID
}
into groupedTable
select new ActivityInfoSummary_DTO
{
recId = Guid.NewGuid(),
Context = groupedTable.Key.ProcessName,
ContextId = groupedTable.Key.ContextID,
SupervisorId = groupedTable.Key.SupervisorID,
FocusCount = groupedTable.Sum(r => r.FocusCount),
many more fields....
}).ToList();
难题是: urls.identityId
是创建记录的人的ID。
创建记录的人可以属于多个组
每个小组只有一名主管
每个主管可以是多个组的主管
一个人可以属于多个组
linq语句试图根据人员创建的记录过滤该人员创建的记录,该人是主管人员管理的组的成员(主管人员ID是groupSup过滤器中的loggingInID字段)。
如果某人是管理人员管理的多个组的成员,则记录将被多次报告,并且该数字将被夸大。
这是我的测试用例之一:(我的问题是,我应该如何进行重组,以便如果主管管理多个组,则向他们报告的所有人员仅记录一次-因此,属于由同一主管管理的两个或多个组的人员他们的数据只报告过一次?
提前致谢!!
您可以通过在对象模型中引入另一个ID来做到这一点。 因此,在您的第一条声明中:
from urls in this.ObjectContext.Activities.AsExpandable().Where(predicateA)
.Where(x => x.GroupID == groupSelectionID)
.Where(r => (r.StartDate >= beginDate && r.StartDate <= endDate) ||
(r.EndDate >= beginDate && r.EndDate <= endDate) ||
(r.StartDate <= beginDate && r.EndDate >= endDate))
在该解决方案中,x只是具有某种上下文类型的组。 这意味着urls集合应具有对象,该对象具有GroupID的member属性。
当您长时间与某物紧密合作时,就会发生这种情况。 我终于弄清楚了-我将查询分为2个步骤,第一步是获取不同身份的列表,然后使用该列表按身份过滤查询。
所以:
var theIDsToInclude = (from id in this.ObjectContext.Identities
join idGroups in this.ObjectContext.IdentityGroups
on id.IdentityID equals idGroups.IdentityID
join groupSup in this.ObjectContext.GroupSupervisors.Where(r => r.SupervisorID == loggedInID)
on idGroups.GroupID equals groupSup.GroupID
select id.IdentityID).Distinct().ToList();
theData = (from urls in this.ObjectContext.Activities.AsExpandable().Where(predicateA)
.Where(r => (r.StartDate >= beginDate && r.StartDate <= endDate) ||
(r.EndDate >= beginDate && r.EndDate <= endDate) ||
(r.StartDate <= beginDate && r.EndDate >= endDate))
.Where(r=> theIDsToInclude.Contains(r.IdentityID))
//join idGroups in this.ObjectContext.IdentityGroups on urls.IdentityID equals idGroups.IdentityID
//join groupSup in this.ObjectContext.GroupSupervisors.Where(r => r.SupervisorID == loggedInID) on idGroups.GroupID equals groupSup.GroupID
join programs in progs on urls.ProcessName.ToUpper() equals programs.ProcessName.ToUpper() into jt
from jt1 in jt.DefaultIfEmpty().Where(r => r == null || r.Ignore == false)
group urls by new { urls.ProcessName, urls.ContextID, jt1.CustomCategory, jt1.Name } into groupedTable
select new ActivityInfoSummary_DTO
{
recId = Guid.NewGuid(),
Context = groupedTable.Key.ProcessName,
ContextId = groupedTable.Key.ContextID,
FocusCount = groupedTable.Sum(r => r.FocusCount),
many more fields...
}).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.