1
var EmpRecList = (from ur in db.Users
  join ug in db.UserGroups on ur.UserGroupID equals ug.UserGroupID
  select new
  {
      lastName = ur.LastName, 
      userID = ur.UserID,
      firstName = ur.FirstName,
      userGroupName = ug.UserGroupNameLang1
  }).Where(oh => oh.userGroupName.StartsWith(userCur.UserGroupName))
                                  .OrderBy(x => x.lastName);

I have this code snippet. The problem here is, im getting 2 records with the same user ID. I would like to have a distinct record based on the User ID. Thanks. Tried using distinct method but no success.

Amiram Korach
  • 13,056
  • 3
  • 28
  • 30
JYR
  • 109
  • 4
  • 15

3 Answers3

1

You can use GroupBy and than get First record. It will get the first record which is in EmpRecList according to userid after ordering, but it will not ensure the result which you want to get.

Try this

var EmpRecList = (from ur in db.Users
                  join ug in db.UserGroups on ur.UserGroupID equals ug.UserGroupID
                  select new
                  {
                      lastName = ur.LastName, 
                      userID = ur.UserID,
                      firstName = ur.FirstName,
                      userGroupName = ug.UserGroupNameLang1
                   })
                  .Where(oh => oh.userGroupName.StartsWith(userCur.UserGroupName))
                  .GroupBy(g => g.userID).Select(s => s.First()).ToList().OrderBy(x => x.lastName)
JYR
  • 109
  • 4
  • 15
Yograj Gupta
  • 9,811
  • 3
  • 29
  • 48
  • The problem here is that the results will vary depending on the ordering of the data. It will give inconsistent results. – Dave New Nov 08 '12 at 13:51
0

The problem here is that you wan't a distinct list of Users BUT your LINQ query is grouping some Users with more than one UserGroups. Performing a distinct on this will not give you a unique list because of different UserGroups.

You need to solve this in your where clause. It needs to be more specific. Instead of your predicate being StartsWith, rather use Equals.

var EmpRecList = (from ur in db.Users
  join ug in db.UserGroups on ur.UserGroupID equals ug.UserGroupID
  select new
  {
      lastName = ur.LastName, 
      userID = ur.UserID,
      firstName = ur.FirstName,
      userGroupName = ug.UserGroupNameLang1
  }).Where(oh => oh.userGroupName.Equals(userCur.UserGroupName))
    .OrderBy(x => x.lastName);

It would actually be better to compare the UserGroups by an ID instead of the name.

Dave New
  • 38,496
  • 59
  • 215
  • 394
0

You can skip the join, so a user with more than one group won't appear twice. I Assumed there is no navigation property to groups, but if there is you can just use ur.UserGroups and you don't need the let definition.

var EmpRecList = (from ur in db.Users
  let groups = db.UserGroups.Where(ug => ur.UserGroupID == ug.UserGroupID)
  select new
  {
      lastName = ur.LastName, 
      userID = ur.UserID,
      firstName = ur.FirstName,
      userGroupNames = groups.Select(g => g.UserGroupNameLang1)
  }).Where(oh => oh.userGroupNames.Any(n => n.StartsWith(userCur.UserGroupName)))
                                  .OrderBy(x => x.lastName);
Amiram Korach
  • 13,056
  • 3
  • 28
  • 30