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 - C#

Enjoy this article? Let's keep in touch

I really appreciate you taking the time out of your schedule to read this post. I like to keep my friends updated on new articles and also spawn discussions here and there.

If you'd like to keep in touch, please subscribe below. And I promise, no more than 1 email per week (if even that).