Consider we have these two entities and one custom object :
public class Entiy1
{
public int Id { get; set; }
public int DestinationId { get; set;
Using LINQ extensions, I'm more of a fan of them:
var results = entityList1
.GroupBy(e => e.DestinationId)
.Select(e => e.First())
.Join(entityList2, e1 => e1.DestinationId, e2 => e2.DestinationId, (e1, e2) =>
new EntityDTO
{
DestinationId = e1.DestinationId,
DestinationName = e2.DestinationName,
JobTitle = e1.JobTitle,
Name = e1.Name
});
Same thing as Gert's anwser really. You can use Distinct
but, you would have to inherit from IEquatible
and implement the Equals
method and override the GetHashCode
method to get it to work.