5

Main Question :
Ok guys, here 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 "fewest code lines" way to retrieve strings in foo containing strings in bar ?
(i.e, in this case, the 2 first elements of foo)

Of course I want to avoid doing all the logic "by hand" as I am sure Linq is more efficient with the intersect feature, but I do not know how to use it to perform this precise task.

TieBreakers :
1 - What if foo contains strings that contain more that one element of bar,

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

And I want to avoid duplicates ?

2 - What if I have 2 "bar" arrays that I want to check against one foo array ?
Is it more efficient to merge bar1 and bar2 or to perform the filter with bar1 first and then with bar2 ?


Thanx and have fun answering :-)

3 Answers 3

6

LINQ works well, yes:

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

Note that you'll still only see each element of foo once, if at all.

If you have multiple bar arrays, you can use:

var mixed = foo.Where(x => bar1.Concat(bar2)
                               .Any(y => x.Contains(y));
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming the string in your description is your teacher's typo,

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

I'm not sure what duplicates you mean, that will only show elements in foo once no matter how many times they appear in bar.

As for the 2nd tie breaker, all you have to do is something like this:

var result=foo.Where(w=>bar1.Any(iw=>w.Contains(iw))||bar2.Any(iw=>w.Contains(iw)));

2 Comments

ThanX It works fine but I had to invert iw=>iw.Contains(w) to iw=>w.Contains(wi) to make it work. Also, I changed the || to && (I've chosen your last syntax proposition because it made sense to me, form a code reading perspective.
|| means in either of them, && means in both of them, I assumed you meant the former.
1
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);

2 Comments

Yup this works too (that was the syntax I've been using a couple of years ago) though it's quicker to do without the select and from statements. Thanx.
@Mika, I prefer the query syntax, especially when the queries grow larger than this in size. I find it more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.