开发者

LINQ - array property contains element from another array

开发者 https://www.devze.com 2022-12-25 13:50 出处:网络
I have a object (product), with a property of type \'array\' e.g. product.tags = {\"tag1\",\"tag2\",\"tag9\"}

I have a object (product), with a property of type 'array'

e.g. product.tags = {"tag1","tag2","tag9"}

I have an array of input tags to filter on.

... but this is not quite working:

List<string> filterTags = new List<string>() { "tag1", "tag3" };

var matches = from p in products
  开发者_如何转开发where p.Tags.Contains(filterTags)
  select p;

Any recommendations? Thanks.


What is the Contains really meant to achieve? Do all items in Tags need to exist in filterTags? Or at least one of them? For the latter use Any and for the former use All. Your where line would change to:

where p.Tags.Any(tag => filterTags.Contains(tag))

or

where p.Tags.All(tag => filterTags.Contains(tag))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号