0

I'm trying to determine a way to find all words in a database table called 'Word' that contain certain letters which is easy enough for single instances of letters, for example if I want to find all words that contain 'L' and 'I' I would use:

Words.Where(w => w.Word_value.IndexOf("I") > 0 && w.Word_value.IndexOf("L") > 0)

However, if I needed to find all words containing the letter 'I' and three instances of the letter 'L' (e.g. 'LILLY'), I am at a loss. Is there a way I can do a count of instances of a string within another?

Any help would be greatly appreciated.

Andy Evans
  • 6,997
  • 18
  • 72
  • 118
  • 1
    duplicate my friend. i am not marking it for closing anyway. http://stackoverflow.com/questions/9144169/using-linq-to-count-substrings-in-a-string and http://stackoverflow.com/questions/541954/how-would-you-count-occurences-of-a-string-within-a-string-c – naveen Mar 15 '12 at 15:05

2 Answers2

2

Try this.

int count = Words.Length - Words.Replace("L", "").Length;
naveen
  • 53,448
  • 46
  • 161
  • 251
  • 1
    I hope you checked why i stick to this method. http://stackoverflow.com/questions/541954/how-would-you-count-occurences-of-a-string-within-a-string-c – naveen Mar 15 '12 at 15:22
1

I would recommend using SQL Server's full text search capabilities.

Create a stored procedure that implements the functionality you require using full text search and then expose that using Linq to Sql or Linq to Entities.

Also see the CONTAINS predicate.

Phil
  • 42,255
  • 9
  • 100
  • 100