Matt
Matt

Reputation: 26981

Complex string matching with LINQ to Entity Framework

We are using LINQ to EF to develop a solution.

In my LINQ query I would like

string toMatch = "Matt Cool";

newString = toMatch.Replace(" ", "% ") + "%"; 

// So here newString is 'Matt% Cool%'

/*Now in my datasource, there is no 'Matt Cool', there's 'Matthew Coolguy'.
I would like this query to return that result. 
I would expect this behavior with the 
wildcard. It doesn't work*/

var results = from a in mycontainer.users
              where a.fullname.equals(newString)
              select a.fullname;

I tried "*" as a wildcard and a regex solution, to no avail -- Are there any other options?

Upvotes: 1

Views: 3846

Answers (2)

IT Helper
IT Helper

Reputation: 69

There's a great article that describes how to do it: Regex in entity framework

Upvotes: -1

Stan R.
Stan R.

Reputation: 16065

instead of using Equals try using Contains this should take your wild cards because internally LINQ uses LIKE when you use Contains

var results = from a in mycontainer.users
              where a.fullname.Contains(newString)
              select a.fullname;

Upvotes: 6

Related Questions