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
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();
Try this
var recipients = (from recipient in recipientCandidates
where recipient.RoleTypeId == roleTypeId orderby recipient.LastName
select recipient).Distinct();
return recipients.ToList();
var recipients = (from recipient in recipientCandidates
where recipient.RoleTypeId == roleTypeId orderby recipient.LastName
select recipient).Distinct();
return recipients.ToList();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Fernando,
That does seem to be the issue. However with this code I can no longer return an object of type List<MessageRecipientCandi date> . I get the following error:
cannot convert from System.Collections.Generic .IEnumerab le<Anonymo usType#1>' to System.Collections.Generic .List<Mess ageRecipie ntCandidat e>
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
That does seem to be the issue. However with this code I can no longer return an object of type List<MessageRecipientCandi
cannot convert from System.Collections.Generic
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();
}
Hi Cragly;
What is MessageRecipientCandidateC ollection and how is this collection populated, please show code.
MessageRecipientCandidateC ollection recipientCandidates
Fernando
What is MessageRecipientCandidateC
MessageRecipientCandidateC
Fernando
ASKER
The MessageRecipientCandidate has the following feilds with the usual simple setters and getters:
A MessageRecipientCandidateC ollection is just a list of MessageRecipientCandidate objects in a generic list: List<MessageRecipientCandi date>
Hope thats helps.
Your help on this one is much appreciated.
A MessageRecipientCandidateC
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
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);
ASKER
Thats great works like a charm!! Thansk for all your help its much appreciated.
Not a problem, glad to help. ;=)
var recipients = (from recipient in recipientCandidates
where recipient.RoleTypeId == roleTypeId orderby recipient.LastName
select recipient.UserId).Distinct
return recipients.ToList();