How to match two separated commas with LINQ

I am new to LINQ.

I am trying to compare two comma separated strings to see if they contain the corresponding value.

I have a line that contains a list of codes. masterFormList = "AAA, BBB, CCC, FFF, GGG, HHH"

I am trying to compare it with a list of objects. In this field, FormCode contains a string with codes separated by commas. I want to see if it is for rent one code in this line is in masterFormList. How to write linq for this?

Now I have:

resultsList = (from r in resultsList where r.FormCodes.Split(',').Contains(masterFormList) select r).ToList(); 

It does not return any matching items from the list.

Please inform

+4
source share
3 answers

You need to put together a collection of items to search for, and then check to see if this set contains:

 var masterSet = new HashSet<string>(masterFormList.Split(',')); resultsList = resultsList .Where(r => r.FormCodes.Split(',') .Any(code => masterSet.Contains(code))) .ToList(); 
+7
source
 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 relevant elements

 var matches = otherList.Split(',').Intersect(masterFormList.Split(',')).ToList(); 
+4
source

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.)

0
source

Source: https://habr.com/ru/post/1495787/


All Articles