簡體   English   中英

避免LINQ中的記錄重復

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM