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) ||
u.LastName.Contains(searchParameter)
select u;
Hope this helps if you ever run into this problem.
About Kevin
Kevin Griffin has been running production .NET applications and teaching developers for over two decades. A 16-time Microsoft MVP specializing in ASP.NET Core and Azure, Kevin brings real-world experience from his own SaaS products alongside his consulting work at Swift Kick. He's hosted the Hampton Roads .NET User Group since 2009 and founded RevolutionVA, the nonprofit behind Hampton Roads DevFest. Kevin's talks blend hard-won lessons from production systems with practical advice you can use Monday morning.
Categories