score:12

Accepted answer

you'd need to build a collection of the items to search for, then check to see if there are any contained within that set:

var masterset = new hashset<string>(masterformlist.split(','));

resultslist = resultslist
                 .where(r => r.formcodes.split(',')
                              .any(code => masterset.contains(code)))
                 .tolist();

score:0

to answer the question as stated, this will find all the matches between two strings:

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 that's overkill if all you want to know is that one exists. you could just do this with the same query:

console.writeline(matches.any());

however, that's likely to do more work than strictly necessary. a modification to reed copsey's answer might be simplest (if we're looking to answer the question in the title of your post):

var masterset = new hashset<string>(masterformlist.split(','));
bool atleastonematch = formcodes.split(',').any(c => masterset.contains(c));

while these are reasonably idiomatic linq solutions to the problem you stated ("i am trying to compare 2 comma separated strings to see if they contain a matching value") they're probably not a great match for what you actually seem to want, which is to take a list of objects, and find just the ones in which a particular property meets your criteria. and a join is probably the wrong approach for that because it looks rather unwieldy:

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 solutions have little to commend them...

so given the problem evident from your code (as opposed to the problem defined in the question title and the first 3 paragraphs of your post), reed copsey's solution is better.

the one tweak i'd make is that if your master set is fixed, you'd only want to build that hashset<string> once, to amortize the costs. so either you'd put it in a static field:

private readonly static hashset<string> masterset =
    new hashset<tring>(masterformlist.split(',');

or use lazy<t> to create it it on demand.

(edited 8/8/2013 after reed pointed out to me in the comments that the problem evident from the code example was not the same as the problem stated in the question.)

score:4

var masterformlist = "aaa,bbb,ccc,fff,ggg,hhh";
var otherlist = "xxx,bbb,yyy";

bool match = otherlist.split(',').intersect(masterformlist.split(',')).any();

or if you want the matching items

var matches = otherlist.split(',').intersect(masterformlist.split(',')).tolist();

Related Query

More Query from same tag