问题
Possible Duplicate:
How to do SQL Like % in Linq?
Like Operator in Entity Framework?
I'm doing a query like this:
var matches = from m in db.Customers
where m.Name == key
select m;
But I don't need m.Name to be exactly equal to key. I need m.Name to be like key.
I can't find how to recreate the SQL query:
WHERE m.Name LIKE key
I'm using SQL Server 2008 R2.
How to do it?
Thanks.
回答1:
Would something like this work for you.. ?
var matches = from m in db.Customers
where m.Name.Contains(key)
select m;
this should also work I edited my answer
回答2:
var matches = from m in db.Customers
where m.Name.StartsWith(key)
select m;
Make the search and compare whether the string is either lowercase or uppercase to get the best result since C# is case-sensitive.
var matches = from m in db.Customers
where m.Name.ToLower().StartsWith(key.ToLower())
select m;
来源:https://stackoverflow.com/questions/11786664/like-query-with-entity-framework