Search code examples
c#linqmorelinq

DistinctBy but ignore null/empty


I have a list of movies, and I need to merge them with another list and duplicate.

I am using Jon Skeet's DistinctBy(m => m.SomeUniqueMovieProperty) to achieve this and it works OK. Except, we soon discovered that there would be cases where 10-20% of the movies (in either list) don't have this property filled out, causing DistinctBy to collapse them into 1 lucky movie.

This is a problem, we want to keep all those movies that don't have a value for this property. Initially I thought of extracting these movies from each collection, duplicating, then merging them again, is there a shorter solution to this problem?


Solution

  • Concatenate the results of DistinctBy() with the results of Where([null or empty]).

    var nullMovies = allMovies.Where(m=>string.IsNullOrEmpty(m.SomeUniqueMovieProperty));
    
    var distinctNonNullMovies = allMovies.Where(m => !string.IsNullOrEmpty(m.SomeUniqueMovieProperty)).DistinctBy(m => m.SomeUniqueMovieProperty);
    
    var result = nullMovies.Concat(distinctNonNullMovies);