Link to home
Start Free TrialLog in
Avatar of Cragly
Cragly

asked on

LINQ Distinct

Hi all,

Does anybody know how to return a distinct list from a LINQ query? I have a generic list called recipientCandidates and I need to select all items out of this list where the roleTypeId is that of particular value. However this returns multiple users with the same Ids so I need to then somehow filter this list by only returning distinct records by userId. I have tried with the query below but it does nothing and is still returning multiple users.

Any ideas experts?? Your help on this would be much appreciated

var recipients = from recipient in recipientCandidates
                 where recipient.RoleTypeId == roleTypeId orderby recipient.LastName
                 select recipient;
return recipients.Distinct().ToList();

Open in new window

Avatar of Irzana
Irzana
Flag of Sri Lanka image

Try this

 var recipients = (from recipient in recipientCandidates
                 where recipient.RoleTypeId == roleTypeId orderby recipient.LastName
                 select recipient.UserId).Distinct();
return recipients.ToList();
Try this

 var recipients = (from recipient in recipientCandidates
                 where recipient.RoleTypeId == roleTypeId orderby recipient.LastName
                 select recipient).Distinct();
return recipients.ToList();
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cragly
Cragly

ASKER

Thanks Fernando,

That does seem to be the issue. However with this code I can no longer return an object of type List<MessageRecipientCandidate> . I get the following error:

cannot convert from System.Collections.Generic.IEnumerable<AnonymousType#1>'to System.Collections.Generic.List<MessageRecipientCandidate>

I am presuming that the return type from your query does not return a know type and can therefore not be cast. I realy need i to return that type as there are a lot of other methods that rely on that type being returned throughout the application.

Any ideas on this one?

Many thanks

Cragly
private static List<MessageRecipientCandidate> GetRecipientListBoxDataSource(MessageRecipientCandidateCollection recipientCandidates, Int32 roleTypeId)
{
    var recipients = from recipient in recipientCandidates
                     where recipient.RoleTypeId == roleTypeId
                     group recipient by recipient.UserId into user
                     let userRec = user.First()
                     orderby userRec.LastName
                     select userRec;
 
    return recipients.ToList();
}

Open in new window

Hi Cragly;

What is MessageRecipientCandidateCollection and how is this collection populated, please show code.

MessageRecipientCandidateCollection recipientCandidates

Fernando
Avatar of Cragly

ASKER

The MessageRecipientCandidate has the following feilds with the usual simple setters and getters:
A MessageRecipientCandidateCollection is just a list of MessageRecipientCandidate objects in a generic list: List<MessageRecipientCandidate>

Hope thats helps.

Your help on this one is much appreciated.


private Guid userId = Constants.NullValue.NullGuid;
private String title = Constants.NullValue.NullString;
private String firstName = Constants.NullValue.NullString;
private String lastName = Constants.NullValue.NullString;
private String fullName = Constants.NullValue.NullString;
private Guid roleId = Constants.NullValue.NullGuid;
private String roleName = Constants.NullValue.NullString;
private Int32 roleTypeId = Constants.NullValue.NullInt;
private String roleTypeTitle = Constants.NullValue.NullString;
private String locationTitle = Constants.NullValue.NullString;
private Int32 locationId = Constants.NullValue.NullInt;
 
#endregion
 
#region Properties
 
public Guid UserId
{
    get { return userId; }
    set { userId = value; }
}
 
public String Title
{
    get { return title; }
    set { title = value; }
}
 
public String FirstName
{
    get { return firstName; }
    set { firstName = value; }
}
 
public String LastName
{
    get { return lastName; }
    set { lastName = value; }
}
 
public String FullName
{
    get { return fullName; }
    set { fullName = value; }
}
 
public Guid RoleId
{
    get { return roleId; }
    set { roleId = value; }
}
 
public String RoleName
{
    get { return roleName; }
    set { roleName = value; }
}
 
public Int32 RoleTypeId
{
    get { return roleTypeId; }
    set { roleTypeId = value; }
}
 
public String RoleTypeTitle
{
    get { return roleTypeTitle; }
    set { roleTypeTitle = value; }
}
 
public String LocationTitle
{
    get { return locationTitle; }
    set { locationTitle = value; }
}
 
public Int32 LocationId
{
    get { return locationId; }
    set { locationId = value; }
}
 
#endregion

Open in new window


Then the function signature should look like this:
 
private static List<MessageRecipientCandidate> GetRecipientListBoxDataSource(List<MessageRecipientCandidate>
recipientCandidates, Int32 roleTypeId)
 
Then when you call the function it should be called like this:
 
List<MessageRecipientCandidate> employeeResults = GetRecipientListBoxDataSource(MessageRecipientCandidateCollection, roleTypeId);

Open in new window

Avatar of Cragly

ASKER

Thats great works like a charm!! Thansk for all your help its much appreciated.
Not a problem, glad to help.  ;=)