LIKE query with Entity Framework [duplicate]

时光毁灭记忆、已成空白 提交于 2019-11-29 04:39:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!