简体   繁体   English

避免LINQ中的记录重复

[英]Avoiding record duplication in LINQ

I have a situation where records are being duplicated and I don't know how to deal with it. 我有一个记录被复制的情况,但我不知道如何处理。 Here is the LINQ statement: 这是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();

The dilemma is: urls.identityId is the ID of the person who created the record. 难题是: urls.identityId是创建记录的人的ID。

The person creating the record can belong in more than one group 创建记录的人可以属于多个组

Each group has a single supervisor 每个小组只有一名主管

Each supervisor can be the supervisor of multiple groups 每个主管可以是多个组的主管

A person can belong to multiple groups 一个人可以属于多个组

The linq statements are trying to filter down the records created by a person based on the fact that the person is a member of a group the supervisor manages (supervisor ID is the loggedInID field in the groupSup filter). linq语句试图根据人员创建的记录过滤该人员创建的记录,该人是主管人员管理的组的成员(主管人员ID是groupSup过滤器中的loggingInID字段)。

If a person is a memberof of multiple groups the supervisor manges, the record is being reported multiple times and the numbers are being inflated. 如果某人是管理人员管理的多个组的成员,则记录将被多次报告,并且该数字将被夸大。

That is one of my test cases :( My question is how do I restructure this so if the supervisor manages multiple groups, all the people reporting to them are recorded only once - so a person belonging to 2 or more groups managed by the same supervisor only has their data reported once? 这是我的测试用例之一:(我的问题是,我应该如何进行重组,以便如果主管管理多个组,则向他们报告的所有人员仅记录一次-因此,属于由同一主管管理的两个或多个组的人员他们的数据只报告过一次?

Thanks in advance!! 提前致谢!!

You maybe able to do this by just introducing another ID into the object model. 您可以通过在对象模型中引入另一个ID来做到这一点。 So during your first piece of the statement: 因此,在您的第一条声明中:

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))

In that solution, x is just the group that has some type of context. 在该解决方案中,x只是具有某种上下文类型的组。 That would mean the urls collection should have objects that have a member property of GroupID. 这意味着urls集合应具有对象,该对象具有GroupID的member属性。

This is what happens when you work too closely with something for too long. 当您长时间与某物紧密合作时,就会发生这种情况。 I finally figured it out - I broke the query down into 2 steps, first step gets a list of distinct identities and then uses that list to filter the query by identity. 我终于弄清楚了-我将查询分为2个步骤,第一步是获取不同身份的列表,然后使用该列表按身份过滤查询。

So: 所以:

            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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM