Take the 2-minute tour ×

Hi 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

share|improve this question
What would make it remove the last row rather than the one with GrpId=6? –  Jon Skeet Aug 10 '10 at 6:10
ok, i need result with distinct Employeeids any one can be removed –  Tassadaque Aug 10 '10 at 6:13

1 Answer 1

up vote 13 down vote accepted

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);
share|improve this answer
just for curiosity! what if i want to remove specific row based on groupid. –  Tassadaque Aug 10 '10 at 6:54
@Tassadaque: You'd have to give more details about what you want - but you could potentially use Except or Where. –  Jon Skeet Aug 10 '10 at 7:27

Your Answer


