LINQ - 数组属性包含另一个数组的元素

时间:2010-03-29 16:09:26

标签: .net linq arrays filter

我有一个对象(产品),其属性类型为'array' 例如product.tags = {“tag1”,“tag2”,“tag9”}

我有一系列输入标签可供过滤。

......但这不太有效:

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

var matches = from p in products
  where p.Tags.Contains(filterTags)
  select p;

有什么建议吗? 感谢。

1 个答案:

答案 0 :(得分:24)

Contains到底意味着什么? Tags中是否需要filterTags中的所有项目?或者至少其中一个?后者使用Any,前者使用All。您的where行将更改为:

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

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