How do you use LINQ to find the duplicate of a specific property?

Reformulated: “you want to group the list of customers by last name, filter out the groups with more than one element, and then select each instance of each group”. You can almost literally translate this sentence to C#:

 customers
    .GroupBy(customer => customer.LastName)
    .Where(sameLastName => sameLastName.Skip(1).Any())
    .SelectMany(customer => customer)

or with LINQ syntax:

var q = from customer in customers
        group customer by customer.LastName into sameLastName
        where sameLastName.Skip(1).Any()
        from customer in sameLastName
        select customer;

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)