tags:

views:

70

answers:

2

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.

+3  A: 
int count = find.Count(f => list.Any(s => s.Contains(f)));
Yuriy Faktorovich
The first `l` should actually be `list`
Shawn
@Shawn fixed it
Yuriy Faktorovich
+1  A: 
var count = (
  from f in find
  where list.Any(l => f.Contains(l))
  select f).Count();
Marc Gravell
Thanks for the expanded syntax
Shawn