LINQ - array property contains element from another array

前端 未结 1 1756
星月不相逢
星月不相逢 2021-02-13 05:05

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.

...

1条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-13 05:59

    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 讨论(0)
提交回复
热议问题