How to check if rows in an array contain rows from another array

The main question:
Ok guys, this is the situation, let's consider 2 string arrays:

string foo = { "Roses are #FF0000" , "Violets are #0000FF", "Paint it #000000"}
string bar = { "Guns", "Roses", "Violets"}

What is the "smallest code string" way to fetch strings in foo containing strings in a string?
(i.e., in this case, the first 2 elements of foo)

Of course, I want to avoid executing all the logic “manually”, since I am sure that Linq is more efficient with the intersection function, but I do not know how to use it to perform this exact task.

TieBreakers:
1 - What to do if foo contains lines containing more than one panel element,

string foo = { "Roses are #FF0000" , "Violets are #0000FF", "Paint it #000000"}
string bar = { "Roses", "Violets", "are"}

And I want to avoid duplicates?

2 - , 2 "bar" , foo?
bar1 bar2 bar1, bar2?


Thanx : -)

+5
3

LINQ , :

var mixed = foo.Where(x => bar.Any(y => x.Contains(y));

, foo , .

bar, :

var mixed = foo.Where(x => bar1.Concat(bar2)
                               .Any(y => x.Contains(y));
+6

, string ,

var result=foo.Where(w=>bar.Any(iw=>w.Contains(iw)));

, , foo, , bar.

2- , , , :

var result=foo.Where(w=>bar1.Any(iw=>w.Contains(iw))||bar2.Any(iw=>w.Contains(iw)));
+2
string[] foo = new string[] { "Roses are #FF0000", "Violets are #0000FF", "Paint it #000000" };
string[] bar = new string[] { "Guns", "Roses", "Violets" };

var matches = from f in foo
                where bar.Any(b => f.Contains(b))
                select f;

foreach (var m in matches)
    Console.WriteLine(m);
+1
source

All Articles