// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Linq; namespace E { public class Post { public Int32 Id { get; set; } public String Description { get; set; } } public class InvariantCaseInsenstiveStringComparion : IEqualityComparer { public bool Equals(string x, string y) { return string.Compare(x, y, StringComparison.InvariantCultureIgnoreCase) == 0; } public int GetHashCode(string obj) { return obj.ToLowerInvariant().GetHashCode(); } } public class Program { public static void Main(string[] args) { string[] arr = { "crap", "fuck" }; List lst = new List(); lst.Add(new Post{Id=1, Description="This is a test"}); lst.Add(new Post{Id=2, Description="This is crap"}); lst.Add(new Post{Id=3, Description="Scrap iron"}); lst.Add(new Post{Id=4, Description="Fuck"}); lst.Add(new Post{Id=5, Description="fuck"}); foreach(Post p in lst.FindAll(p => arr.Contains(p.Description))) { Console.WriteLine(p.Id + " " + p.Description); } Console.WriteLine("===="); foreach(Post p in lst.Where(p => arr.Any(s => p.Description.Contains(s)))) { Console.WriteLine(p.Id + " " + p.Description); } Console.WriteLine("===="); foreach(Post p in lst.Where(p => arr.Intersect(p.Description.Split(' '), new InvariantCaseInsenstiveStringComparion()).Count() > 0)) { Console.WriteLine(p.Id + " " + p.Description); } } } }