linq returning query with a list of ids

344 views Asked by At

I'm doing a filter and I need all the values that has for example (rock = id 3 and jazz = id 6)

How can I do that in LINQ ?

I'm doing this but this gives first the one with rock and the with jazz....

private IQueryable<Performance>addGenreFilterToPerformanceQuery(IQueryable<Performance> query, List<int> genreFilters)
{
    if (genreFilters != null)
    {
        foreach (var genreFilter in genreFilters)
        {
            return query.Where(p => p.Artist.Genres.Any(g => genreFilters.Contains(g.Id)));
        }
    }
    return query;
}

Any suggestions?

1

There are 1 answers

2
Charles Mager On

The foreach isn't really doing anything. If you want the query to match all genre ids rather than any, use All, not Any:

return query.Where(p => p.Artist.Genres.All(g => genreFilters.Contains(g.Id)));