John
John

Reputation: 1327

LINQ - UNION - remove duplicated based on a single column

I have this linq query where i want to remove all the duplicates based on the column MAILADR (but keep the other columns):

    Dim dataObject = (From a In db.TABLE1 Select New With {
        .ID = a.BENUTZERNR,
        .MAILADR = a.EMAIL,
        .BENUTZERGRP = a.USRGRP
    }) _
    .Union(
        (From b In db.TABLE12 Select New With {
        .ID = b.ID,
        .MAILADR = b.MAILADR,
        .BENUTZERGRP = b.BENUTZERGRP 
    }) _
    )

Upvotes: 1

Views: 809

Answers (2)

John
John

Reputation: 1327

I ended up using the GroupByoperator:

dataObject = dataObject.GroupBy(Function(c) c.MAILADR).Select(Function(group) group.First())

Upvotes: 0

Serge
Serge

Reputation: 6692

You can provide a comparison method (IEqualityComparer) on the overloaded Union method.

http://msdn.microsoft.com/en-us/library/bb358407.aspx

Upvotes: 3

Related Questions