Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial Matches in a String[]

Tags:

c#

linq

I have two string arrays; one is list and the other is find

I want to be able to count the number of items in find that are partially contained within 'list' using extension methods and linq. Here is a summary of how I would do it within a few nested loops:

int Count = 0;

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

return Count;

I'd like to be able to do something like:

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

In my actual application, list is an element within a linq query of type IQueryable<string> and find is a static string[]. I'd like to be able to perform the count above within linq. I know I will probably have to use .AsEnumerable() as whatever the solution is probably wont be able to be translated to SQL.

like image 539
Shawn Avatar asked Sep 05 '26 15:09

Shawn


1 Answers

int count = find.Count(f => list.Any(s => s.Contains(f)));
like image 186
Yuriy Faktorovich Avatar answered Sep 08 '26 03:09

Yuriy Faktorovich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!