Home > Software engineering >  Linq: Grouping elements with a condition
Linq: Grouping elements with a condition

Time:09-04

I have this list-

    public class SecretAgents
    {
        public string Name {get; set;}
        public List<string> SecretData {get; set;}
        public int GroupID {get; set;}
    }

    List<SecretAgents> secretAgents = new List<SecretAgents>();

    // Each item has a total of 256 elements in it under the
    // SecretData list of string that consists of "apple" and "orange"

    secretAgents.Add(new SecretAgents { Name= "SecretAgent 1", GroupID= 0, SecretData= new List<string> { "orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","apple","apple","apple","apple","apple","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","orange","apple","apple","apple","apple","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","apple","orange","apple","apple","apple","orange","orange","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","orange","apple","apple","apple","orange","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","apple","apple","apple","apple","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","apple","apple" } });
    secretAgents.Add(new SecretAgents { Name= "SecretAgent 2", GroupID= 0, SecretData= new List<string> { "apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","orange","orange","orange","orange","apple","apple","orange","orange","apple","orange","orange","orange","apple","apple","apple","orange","orange","orange","orange","apple","apple","apple","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","orange","orange","orange","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","apple","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange" } });
    secretAgents.Add(new SecretAgents { Name= "SecretAgent 3", GroupID= 0, SecretData= new List<string> { "orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","apple","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","orange","apple","apple","apple","apple","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","apple","orange","apple","apple","apple","orange","orange","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","orange","apple","apple","apple","orange","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","orange","apple","apple","apple","orange","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","orange","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","apple","apple","apple","apple","orange","orange","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","apple","orange","apple","apple" } });
    secretAgents.Add(new SecretAgents { Name= "SecretAgent 4", GroupID= 0, SecretData= new List<string> { "orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","orange","orange","orange","orange","orange","apple","orange","orange","apple","orange","orange","orange","apple","apple","apple","orange","orange","orange","orange","apple","apple","apple","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","apple","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","apple","apple","apple","orange","orange","orange","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","apple","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange","apple","orange","orange","orange","orange","apple","apple","orange","orange","orange","orange","orange","orange","orange","orange","orange" } });

Equal elements ("apple" and "orange") between different items:

  • Secret Agent 1 & Secret Agent 3: 248
  • Secret Agent 2 & Secret Agent 4: 256

In rest other possibilities (for example: Agent 1 & Agent 3, Agent 2 & Agent 3, etc.), equal elements are lesser than 200.

Now, what I want is to group all items (Secret Agents) that have a minimum of 200 equal elements ("apple" and "orange").

Expected Results

Secret Agent 1: Group ID 1

Secret Agent 3: Group ID 1

Secret Agent 2: Group ID 2

Secret Agent 4: Group ID 2

Pseudo Code:

Iterate through each item in the secretAgents list and then use Linq Zip function:

    int index=0;    
    foreach(var item in secretAgents)
    {
        index  ;
        int equalElements = item.Zip(item, (i, j) => i.SecretData == j.SecretData).Count(eq => eq);
        if (equalElements>200)
        {
            item.GroupID=index;
        }
    }

CodePudding user response:

That's a painful query:

var query =
    from q in
    (
        from sa1 in secretAgents
        from sa2 in secretAgents
        where sa1.Name.CompareTo(sa2.Name) == -1
        let count = sa1.SecretData.Zip(sa2.SecretData, (First, Second) => (First, Second)).Where(x => x.First == x.Second).Count()
        where count > 200
        group new[] { sa1, sa2 } by count
    ).Select((xss, i) => (xss, i))
    from xs in q.xss
    from x in xs
    select new { Name = x.Name, GroupID = q.i   1 };

It gives me:

query

  • Related