C# 使用实体框架进行 LIKE 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11786664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LIKE query with Entity Framework
提问by
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.Nameto be exactly equal to key. I need m.Nameto be like key.
但我不需要m.Name完全等于 key。我需要m.Name像钥匙一样。
I can't find how to recreate the SQL query:
我找不到如何重新创建 SQL 查询:
WHERE m.Name LIKE key
I'm using SQL Server 2008 R2.
我正在使用 SQL Server 2008 R2。
How to do it?
怎么做?
Thanks.
谢谢。
采纳答案by MethodMan
Would something like this linq query work for you.. ?
这个 linq 查询对你有用吗..?
var matches = from m in db.Customers
where m.Name.Contains(key)
select m;
this should also work I edited my answer.
这也应该有效我编辑了我的答案。
Containsis mapped to LIKE '%@p0%' which is case insensitive
Contains映射到不区分大小写的 LIKE '%@p0%'
回答by Andrew Cooper
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.
进行搜索并比较字符串是小写还是大写以获得最佳结果,因为 C# 区分大小写。
var matches = from m in db.Customers
where m.Name.ToLower().StartsWith(key.ToLower())
select m;

