To answer the question as indicated, this will find all matches between the two lines:
var matches = from masterCode in masterFormList.Split(',') join formCode in formCodes.Split(',') on masterCode equals formCode select formCode; foreach (string match in matches) { Console.WriteLine(match); }
but itβs superfluous if all you want to know is that it exists. You can simply do this with the same query:
Console.WriteLine(matches.Any());
However, this is likely to do more work than necessary. Modifying Reed Copsi's answer could be simpler (if we want to answer the question in the header of your message):
var masterSet = new HashSet<string>(masterFormList.Split(',')); bool atLeastOneMatch = formCodes.Split(',').Any(c => masterSet.Contains(c));
Although these are reasonably idiomatic LINQ solutions for the problem you indicated ("I'm trying to compare two strings separated by a comma to see if they contain the corresponding value"), they are probably not very suitable for what you actually want, to take a list of objects and find only those in which a particular property meets your criteria. And join is probably the wrong approach to this because it looks rather cumbersome:
resultList = (from formItem in resultList from code in formItem.FormCodes.Split(',') join masterCode in masterFormList.Split(',') on code equals masterCode group code by formItem into matchGroup select matchGroup.Key) .ToList();
or if you prefer:
resultList = (from formItem in resultList from code in formItem.FormCodes.Split(',') join masterCode in masterFormList.Split(',') on code equals masterCode into matchGroup where matchGroup.Any() select formItem) .Distinct() .ToList();
These decisions have little chance of praising them ...
So, given the problem obvious from your code (as opposed to the problem defined in the title of the question and the first three paragraphs of your post), Reed Copsi's solution is better.
The only thing I would do is that if your core set is fixed, you only want to build this HashSet<string> once to amortize costs. So either you put it in a static field:
private readonly static HashSet<string> masterSet = new HashSet<tring>(masterFormList.Split(',');
or use Lazy<T> to create it on demand.
(Edited 08/08/2013 after Reid pointed out to me that the problem obvious from the sample code does not match the problem asked in the question.)