首页 > 解决方案 > Linq 与部分匹配相交

问题描述

对于 Linq 查询中的部分单词匹配,如何让“相交”返回 true?我需要一种.Contains() .Intersect()混合体。

 List<string> sParams = new List<string>(){"SAND", "PURPLE"};

 //One of my Prices has the color "Sanddust"
 Prices.Where(x => x.Color.ToUpper().Split(null).Intersect(sParams).Any());

上面的查询只返回精确的字符串匹配相交,但我需要返回 true,因为字符串“SANDDUST”包含“SAND”。

标签: c#linq

解决方案


为什么需要相交?只需使用任何包含。

Prices.Where(x => sParams.Any(s => x.Color.ToUpper().Contains(s)));

推荐阅读