Check if a string within an array appears in another string array in LINQ?

Var first = new {"the", "quick", "brown"}; var second = new {"This","doesnt","quick","Work"}; var found = from a in first from be in second where a == be select a; if(found.Any()) return true.

This is O(n*m) whereas using Intersect should be O(n+m). It won't matter for examples like this, but could make a big difference for larger sequences. – LukeH Mar 22 at 12:25 ok thanks, which is more efficient then?

– jaffa Mar 22 at 12:41 @Jon: Using Intersect is more efficient (although probably not noticeably so if the sequences are small). – LukeH Mar 22 at 12:48 @LukeH: Ok great, how would this work for case-insensitivity too? – jaffa Mar 22 at 12:55 @Jon: Intersect can take an IEqualityComparer to handle that.

For example, bool exists = first. Intersect(second, StringComparer. OrdinalIgnoreCase).Any(); or similar.Msdn.microsoft.Com/en-us/library/bb355408.

Aspx – LukeH Mar 22 at 14:34.

Bool exists = first. Intersect(second).Any(); Or, if you want to know what the common words actually are: var commonWords = first. Intersect(second); foreach (string s in commonWords) { Console.

WriteLine(s); }.

I think I'd use: var query = from a in first where b. Contains(a) select a; var isThereAMatch = query.Any(); If the lists were very large and matches were likely to be sparse, then I might optimise this by using a lookup/hashset for b.

This should be pretty simple but LINQ's Contains() doesn't take an array. And I want to compare another array of strings and return true if any string appears in it. So "quick" appears in the 2nd array.

Is it best making the first string joined comma delimited so it looks like "The, quick , Brown" and then running contains in a loop against it? I'm sure this can be done properly using LINQ.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions