Partial matches in string []

I have two string arrays; one listand the otherfind

I want to be able to count the number of elements in a search that are partially contained in a "list" using extension and linq methods. Here is a summary of how I will do this in several nested loops:

int Count = 0;

foreach (string f in find)
{
    foreach (string l in list)
    {
         if (l.Contains(f))
         {
              Count++;
              break;
         }
    }
}

return Count;

I would like to do something like:

int Count = list.Select(...);

In my actual application, listis an element in the linq type request IQueryable<string>and findis static string[]. I would like to be able to execute the account above in linq. I know that I will probably have to use it .AsEnumerable(), since any solution probably cannot be translated into SQL.

+3
source share
2
int count = find.Count(f => list.Any(s => s.Contains(f)));
+3
var count = (
  from f in find
  where list.Any(l => f.Contains(l))
  select f).Count();
+1

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


All Articles