-1

I have a collection of group users which has a GroupId and UserId. I need to filter out any duplicate GroupId/UserId objects which may exist in the collection. How can I write a non-query expression GroupBy to filter out the duplicate rows? The following example is adapted from a group by example that I found online but I'm not quite clear on how to refine this code for my particular scenario:

var groupByResults = 
groupUsers.GroupBy(
    x => x.GroupId,
    x => x.UserId,
    (key, g) => new
    {
        [?] = key,
        [?] = g.ToList()
    }
);
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
user9393635
  • 1,369
  • 4
  • 13
  • 32

3 Answers3

0

If your data looks like the list below you can group by the compound key then take the first value in each group. The OrderBy is optional

var groupUsers = new List<dynamic>() { 
    new { groupId = 1, userId = 1, name = "a" },
    new { groupId = 1, userId = 1, name = "b" },
    new { groupId = 1, userId = 2, name = "c" }
    };

var result =  groupUsers
    .GroupBy(u => new { u.groupId, u.userId} )
    .Select(g => g.OrderBy(u => u.name).FirstOrDefault());
0

To find out the duplicated userId, groupId.

  1. GroupBy userId, groupId

  2. Count if any group item >=2

  3. SelectMany the collection

Code:

var duplicatedUsers = groupUsers
    .GroupBy(gu => new { gu.UserId, gu.GroupId })
    .Where(g => g.Count() >= 2)
    .SelectMany(g => g)
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
ocrenaka
  • 192
  • 2
  • 8
  • `SelectMany(g => g)` undoes the groupby (other than keeping them sorted by their group key), thus defeating OP's likely intention. Use `SelectMany(g => g.Skip(1))` if you want a list of the second/third/fourth/.... occurences of the duplicate value, **but not the first occurrence**. Use `Select(g => g.First())` if you want a list of the first occurence, but not the second/third/fourth/.... – Flater Apr 19 '18 at 08:19
0

Following code will be helpful to you,

class GroupUsers 
{
    public int GroupId {get;set;}
    public int UserId {get;set;}
}

public class Program
{
    public static void Main()
    {
    var groupUsers = new List<GroupUsers>() { 
    new GroupUsers{ GroupId = 1, UserId = 1},
    new GroupUsers{ GroupId = 1, UserId = 1},
    new GroupUsers{ GroupId = 1, UserId = 2},
    new GroupUsers{ GroupId = 1, UserId = 2},
    new GroupUsers{ GroupId = 1, UserId = 3},
    new GroupUsers{ GroupId = 1, UserId = 4},
    new GroupUsers{ GroupId = 1, UserId = 5},
    new GroupUsers{ GroupId = 1, UserId = 3}
    };

     var result1 =  groupUsers
                    .GroupBy(u => new { u.GroupId, u.UserId} )
                    .Where(g => g.Count()>=2) // check for duplicate value by checking whether the count is greater than or equal to 2.
                    .SelectMany(g=>g); // flatten the list

    foreach(var user in result1) // Iterate over the result
    {
        Console.WriteLine(user.GroupId +" "+user.UserId);
    }
    // Or
    var result2 =   from a in groupUsers
                    group a by new{a.GroupId, a.UserId} into grp
                    where grp.Count()>=2
                    from g in grp select new{g}

    foreach(var user in result2)
    {
        Console.WriteLine(user.g.GroupId +" "+user.g.UserId);
    }

    }
}
Abhilash Ravindran C K
  • 1,818
  • 2
  • 13
  • 22