-1

Let say I have a list of objects with the following properties (FirstName, LastName):

var people = new []
{
    new { FirstName = "John", LastName = "Smith" },
    new { FirstName = "Dale", LastName = "Smith" },
    new { FirstName = "Jermey", LastName = "Ducey" },
}.ToList();

Using Linq how can I return just the object containing "Jeremy, Ducey" if I am search by unique last names.

Distinct() still returns one instance of smith, and every answer leads me to GroupBy() which only return groups properties OR Distinct() which doesn't do what I want.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Tronald
  • 1,520
  • 1
  • 13
  • 31
  • Do you literally have that list of strings? Including `"First, Last"` as the first string? Or do you actually have a list of objects with properties `First` and `Last`? – Enigmativity Nov 08 '20 at 02:09
  • Sorry the first is headers I'll edit – Tronald Nov 08 '20 at 02:13
  • So the key you're looking for is last name, having count > 1, distinct. I think that's why answers are leading you to GroupBy as part of the solution. – Mark C. Nov 08 '20 at 02:17
  • I edited the title, to differentiate this question from a [similar one](https://stackoverflow.com/questions/292307/selecting-unique-elements-from-a-list-in-c-sharp "Selecting Unique Elements From a List in C#"). – Theodor Zoulias Nov 08 '20 at 08:44
  • 1
    Hey @Enigmativity! This is a nice question, and it's a pity to be closed as unclear. Could you suggest a way to improve the question so it can be reopened? – Theodor Zoulias Nov 09 '20 at 21:51
  • @TheodorZoulias Classic SO – Tronald Nov 09 '20 at 22:19
  • 1
    Tronald I'm kind of new here, but AFAIK it's getting worse. – Theodor Zoulias Nov 09 '20 at 22:21

1 Answers1

3

This does what you want:

var people = new []
{
    new { FirstName = "John", LastName = "Smith" },
    new { FirstName = "Dale", LastName = "Smith" },
    new { FirstName = "Jermey", LastName = "Ducey" },
}.ToList();

var uniqueLastNames =
    people
        .GroupBy(x => x.LastName)
        .Where(xs => xs.Count() == 1)
        .SelectMany(xs => xs)
        .ToList();

It gives me:

unique

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • AFAIK the [`Count()`](https://referencesource.microsoft.com/system.core/system/linq/Enumerable.cs.html#41ef9e39e54d0d0b) operator is implemented with a fast path for collections, and the [`Grouping`](https://referencesource.microsoft.com/system.core/system/linq/Enumerable.cs.html#7bb231e0604c79e3) is an `IList`, so the `xs.Count() == 1` should be quite efficient. – Theodor Zoulias Nov 08 '20 at 07:33
  • 1
    @TheodorZoulias - Yes, that checks out. :-) – Enigmativity Nov 08 '20 at 08:51