LINQ–search for items in a list that are present in another list

Suppose you have a list of strings – a nursery rhyme may be a good example.  And you need to find ALL the words that match from another list of words.

Using LINQ to search a list for items in another list (should return a collection of all the matching words – “The,”, “Mouse”,”Up”

        var rhyme = "The mouse ran up the clock";
        private string[] wordsToSearchFor =
        {
            "The","mouse","up"
        };

        public IQueryable<string> FindWordsInRhyme()
        {

            var wordsFoundInRhyme = rhyme.Where(a => wordsToSearchFor.Count(i => a.ToString().Contains(i)) == 0);
            return wordsFoundInRhyme;
        }

Cloud Advisory Services | Security Advisory Services | Data Science Advisory and Research

Specializing in high volume web and cloud application architecture, Anuj Varma’s customer base includes Fortune 100 companies (dell.com, British Petroleum, Schlumberger).

All content on this site is original and owned by AdverSite Web Holdings, Inc. – the parent company of anujvarma.com. No part of it may be reproduced without EXPLICIT consent from the owner of the content.

Anuj Varma – who has written posts on Anuj Varma, Technology Architect.


Leave a Reply

Your email address will not be published. Required fields are marked *