1

How can I get 'Intersect' to return true for a partial word match in a Linq query in? I need a .Contains() .Intersect() hybrid of sorts.

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

The above query only returns exact string match intersects, but I need to to return true as the string "SANDDUST" contains "SAND".

Tronald
  • 1,520
  • 1
  • 13
  • 31

2 Answers2

1

Why would you need intersect? Just use an Any Contains.

Prices.Where(x => sParams.Any(s => x.Color.ToUpper().Contains(s)));
Danny
  • 85
  • 5
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • 1
    Perfect thank you. I am not sure why I felt like I needed to split both strings and compare??? One of those days. – Tronald Nov 02 '18 at 23:01
  • Do you need to do case? If so check out this question: https://stackoverflow.com/questions/9923158/c-sharp-contains-part-of-string – Kris.J Nov 02 '18 at 23:13
1
Prices.Where(x => sParams.Any(s=> x.Color.ToUpper().Contains(s));
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Danny
  • 85
  • 5
  • Also if you want to answer questions, its better if you actually describe your solution, how it works, and any other relevant information – TheGeneral Nov 02 '18 at 23:03