简体   繁体   中英

Match all words in any order

How do you match a search string for every word using linq? ie "apple orange" should match on "orange apple" but not "apple orange fred".

This query here works to find if any single word matches, I could not get it to work for All() words matching.

var match = "apple orange pear".Split()
        .Intersect("orange pear fred".Split())
        .Any();

The idea is very similar to this thread. Word-wise super string search for given string

Check if each word exists in check list:

var words = "orange pear fred".Split();
var wordsToCheck = "apple orange".Split();
var match = words.All(w => wordsToCheck.Contains(w));

Or produce difference of two sequences. If there is no elements in difference, then all words are in check list:

var match = !words.Except(wordsToCheck).Any();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM