Wildcard search with LINQ

I just a situation where I needed to perform a wildcard search on a table in my database. When I used to do ADO.NET, I would simply write my SELECT statements with LIKE keywords to do wildcard searches.

However, in this project, I’m using LINQ to Entities and the solution didn’t work the same way as it did back in SQL land. My alternative was to use the .Contains() method.

For example:

var userList = from u in entity.Users
where u.FirstName.Contains(searchParameter) ||
select u;

Hope this helps if you ever run into this problem.

Originally published on 2009-04-21 in Development