13

i have many to many relationship between employee and group. following linq statement

int[] GroupIDs = {6,7};


var result = from g in umGroups
    join empGroup in umEmployeeGroups on g.GroupID equals empGroup.GroupID
    where  GroupIDs.Contains(g.GroupID)                     
    select new {  GrpId = g.GroupID,EmployeeID = empGroup.EmployeeID };

returns groupid and the employeeid. and result is

GrpId  | EmployeeID
6      |   18
6      |   20  
7      |   19
7      |   20

I need to remove the rows for which the employeeid is repeating e.g. any one of the row with employeeid= 20
Thanks

Tassadaque
  • 8,129
  • 13
  • 57
  • 89

1 Answers1

42

Okay, if you don't care which employee is removed, you could try something like:

var result = query.GroupBy(x => x.EmployeeId)
                  .Select(group => group.First());

You haven't specified whether this is in LINQ to SQL, LINQ to Objects or something else... I don't know what the SQL translation of this would be. If you're dealing with a relatively small amount of data you could always force this last bit to be in-process:

var result = query.AsEnumerable()
                  .GroupBy(x => x.EmployeeId)
                  .Select(group => group.First());

At that point you could actually use MoreLINQ which has a handy DistinctBy method:

var result = query.AsEnumerable()
                  .DistinctBy(x => x.EmployeeId);
d219
  • 2,707
  • 5
  • 31
  • 36
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194