This is also a great use case for an extension method ContainsAny like this:
public static class IEnumerableExtensions
{
/// <summary>
/// Determines whether a sequence contains any of the specified items.
/// </summary>
/// <typeparam name="T">The type of the elements in the sequences.</typeparam>
/// <param name="source">The sequence to search.</param>
/// <param name="items">The sequence of items to search for in the source sequence.</param>
/// <returns><c>true</c> if the source sequence contains any of the specified items; otherwise, <c>false</c>.</returns>
/// <remarks>
/// If either the source or items sequence is null, it will be treated as an empty sequence.
/// This method uses a HashSet for efficient lookup of items.
/// </remarks>
public static bool ContainsAny<T>(this IEnumerable<T> source, IEnumerable<T> items)
{
source ??= Array.Empty<T>();
items ??= Array.Empty<T>();
var itemSet = new HashSet<T>(items);
return source.Any(itemSet.Contains);
}
}
And then you can use like this:
var dbMovies = new List<Movie>
{
new Movie("Moana", new [] { "Animation", "Comedy" }),
new Movie("Mulan", new [] { "Animation", "Family" }),
new Movie("Brave", new [] { "Adventure", "Comedy" }),
};
var listOfGenres = new [] { "Comedy", "Western" };
var movies = dbMovies.Where(m => m.Genres.ContainsAny(listOfGenres));
Console.WriteLine(string.Join(", ", movies.Select(m => m.Title)));
// Moana, Brave
Searching for a list in a list in a list is already confusing enough, and this helps abstract some of he parts away to make the code more readable.
See Also: How to use Linq to check if a list of strings contains any string in a list
Genreis defined.