0

I need a function which returns a filtered list of distinct users with different usernames. I want to exclude the users who have similar usernames, but with different passwords.

Input
user1 ("Jim", 1111)
user2 ("Jim", 3333)
user3 ("Bill", 2222)
user4 ("Mike", 2222)

Output
user3 ("Bill", 2222)
user4 ("Mike", 2222)

private IEnumerable<User> GetCorrectUsers(IEnumerable<User> userList)
{
   IEnumerable<User> filtered= userList.GroupBy(user => user.UserName)
      .Select(g => g.???)
      .Where(p => p.Password).Distinct().Count() > 1;
   return filtered;
}

One step is missing.

fota666
  • 141
  • 1
  • 9

1 Answers1

2

from comment of Caius Jard the solution can be:

var filtered = users.GroupBy(x => x.Name)
            .Where(g => g.Select(grElem => grElem.Password).Distinct().Count() == 1).Select(x => x.First())
            .ToList();
  • You haven't considered his password condition at all, He needs to omit users who have similar usernames with different passwords. – Dharani Kumar Oct 03 '18 at 08:40
  • Thanks, it looks okay. I'll test it. All of the users with same name, but different password must be excluded. – fota666 Oct 03 '18 at 10:20