在LINQ中避免记录重复

时间:2015-09-17 04:05:02

标签: c# linq duplicates

我的情况是记录被复制,我不知道如何处理它。这是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过滤器中的loggedInID字段)来过滤人员创建的记录。

如果一个人是管理员管理的多个组的成员,则会多次报告该记录并且数字正在膨胀。

这是我的一个测试用例:(我的问题是我如何重组这个,所以如果主管管理多个组,所有向他们报告的人只被记录一次 - 所以属于2个或更多组的人由同一个主管只报告一次数据?

提前致谢!!

2 个答案:

答案 0 :(得分:0)

您可以通过在对象模型中引入另一个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成员属性的对象。

答案 1 :(得分:0)

当你长时间与某事物密切合作时会发生这种情况。我终于搞清楚了 - 我将查询分解为两步,第一步获取不同身份列表,然后使用该列表按身份过滤查询。

所以:

            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();
相关问题