'Using LINQ to see if a nested property appears in an array of ints

I have a class called Business, a class called Tag and a class called BusinessTagLink.

Both Business and Tag have the following property:

public virtual IList<BusinessTagLink>? BusinessTagLinks { get; set; }

and every business is linked to one or more tags via the BusinessTagLink table.

My BusinessTagLink class looks like this:

public class BusinessTagLink : BaseEntity
    {
        public int BusinessTagLinkId { get; set; }
        public int BusinessId { get; set; }
        public int TagId { get; set; }
        public virtual Business Business { get; set; }
        public virtual Tag Tag { get; set; } 
    }

I am trying to build a filter that will allow a user to filter down the list of businesses by interacting with a list of tags. I'm currently thinking they'll click on the tag and the ID of that tag will then get added to an int array, which will be passed to a filter method along with the full list of potential businesses. Something like:

public static IQueryable<Business> Filter(this IQueryable<Business> businesses, int[] tagIds) { }

What I'm really struggling with is how to use these parameters to return a list of filtered businesses. I think the LINQ query will make use of .Contains() and/or .Any() but I'm clueless beyond that.

return businesses.Where(tagIds.Contains(x => x.BusinessTagLinks.Select(y => y.TagId)))

return businesses.Where(x => x.BusinessTagLinks.Any(y => y.TagId == [no idea how to target array here]));

Any advice would be greatly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source