LINQ - 数组属性包含元素从另一个数组 [英] LINQ - array property contains element from another array

查看:349
本文介绍了LINQ - 数组属性包含元素从另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象(产品),类型为数组的物业买卖资讯
例如product.tags = {TAG1,标签2,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.

推荐答案

什么是包含真正的意思来实现?不要在标签所有项目需要 filterTags 存在吗?或者至少是其中之一吗?对于后者使用任何和前使用所有。你的其中,行将​​更改为:

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))

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

这篇关于LINQ - 数组属性包含元素从另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆