我有一个对象(产品),其属性类型为'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;
有什么建议吗? 感谢。
答案 0 :(得分:24)
Contains
到底意味着什么? Tags
中是否需要filterTags
中的所有项目?或者至少其中一个?后者使用Any
,前者使用All
。您的where
行将更改为:
where p.Tags.Any(tag => filterTags.Contains(tag))
或
where p.Tags.All(tag => filterTags.Contains(tag))