none
C# code snippet to retrive duplicates from collection RRS feed

  • Question

  • HI Team,

    I have below collection that has work item details, currently I am retrieving the WorkItemId(Source and Targetid) using below code.

    WorkItemLinkInfo[] wlinks = query.RunLinkQuery();

    for(WorkItemLinkInfo info in wlinks)
    {
        sourcId = info.SourceId;
        targetId = info.TargetId
    }

    We have duplicate values at targetId, I need to traverse WorkItemLinkInfo and write output to two arrays
    Array1 : Contains source id's that have duplicate targetid.
    Array2: (Contains source id's that have unique targetid) and their respective target id's

    For example:
    If WorkItemlinfo has
    SourceId, TargetId
    s1,a
    s2,b
    s3,a
    s4,a
    s5,c
    s8,c
    s9,a
    s10,x

    Output:

    Array1: S1,S3,S4,S8,S9 [SourceIds that has duplicate targetId a and c]
    Array2: S2, b, S10,x [SourceId that has unique values and their target ids]

    Thanks

    Thursday, December 7, 2017 5:12 AM

Answers

  •          List<WorkItemLinkInfo> unique = new List<WorkItemLinkInfo>();
             List<WorkItemLinkInfo> duplicate = new List<WorkItemLinkInfo>();
    
             foreach (WorkItemLinkInfo wl in wlinks)
             {
                if (wlinks.Count(x => x.TargetID == wl.TargetID) > 1)
                   duplicate.Add(wl);
                else
                   unique.Add(wl);
             }

    Thursday, December 7, 2017 5:35 AM