Recently I had cause to perform a wildcard search in LINQ to SQL.
Originally I had something like:
database.Table.Where(p => p.Column == SomeValue).ToList()
The customer came back with an update to the requirement, as they do. They now wanted a wildcard search not a straight equality test. Being new to LINQ to SQL I had a quick chat with Neil and he pointed me straight at .Contains, .StartsWith and .EndsWith.
I got to wondering what the generated SQL for .Contains, .StartsWith and .EndsWith would look like.
It's fairly simple, and obvious, when you think about it;
.Contains will spit out ..... LIKE '%YourSearchTerm%'
.EndsWith will spit out ..... LIKE '%YourSearchTerm'
.StartsWith will spit out ..... LIKE 'YourSearchTerm%'
A quick Google shows me that David Hayden has already covered this, here.....
There is more to add to this story, however.
Performance.
Any SQL dba worth their salt will tell you that when using the LIKE statement, 'YourSearchTerm%' will perform much better than either of the other two options.
Again, this is fairly simple and obvious when you think about it;
'YourSearchTerm%' will result in an Index seek, the other two will result in an Index scan.
Again, a Google shows that John Kane has already covered this, here.....
Neither of these provide a full end to end picture, now you have one ;-)
So if you need to perform this kind of wildcard search in LINQ to SQL, use .StartsWith.