PrivateJoker
PrivateJoker

Reputation: 791

Linq to search a property of a list based on string array variable

I have an IQueryable of Gizmo.

I want to write LINQ statement that will filter all Gizmos whose color is in the parameter (string[]) that I pass.

For example:

public class Gizmo
{
      public string Id { get; set; }

      public string Name { get; set; }

      public virtual ICollection<Color> Colors { get; set; } = new List<Color>();
}

public class Color
{
      public string Id { get; set; }
      public string Value { get; set; } 
}

I will be supplied a parameter like:

var filterColors = new[] { "red", "silver" };

I want to write something similar to:

gizmos = gizmos.Where(x => x.ColorTags.Contains(filterColors));

Upvotes: 0

Views: 42

Answers (1)

Ousmane D.
Ousmane D.

Reputation: 56469

Seems like you're looking for:

gizmos = gizmos.Where(x => filterColors.Any(z => x.Colors.Any(e => e.Value == z)));

Upvotes: 2

Related Questions