开发者

Like SQL Query in LINQ

My problem is the following:

I'm looking for a solution in LINQ which translates a LINQ expression to SQL LIKE query.

(I know in LINQ I can use Contains, StarsWidth, EndWidth instead of 'LIKE' but in my case is not a good solution, and if you check the generated sql script you will see it is not use LIKE)

I found a lot of article that use the System.Data.Linq.SqlClient.SqlMethods.Like methods so I wrote my query:

var query = from c in _context.prgCity where SqlMethods.Like( c.FullName, "%bud开发者_高级运维a") select c.FullName + "|" + c.prgCountry.CountryName;

return query.ToList<string>();

But when query is running I get the following error message:

"LINQ to Entities does not recognize the method 'Boolean Like(System.String, System.String)' method, and this method cannot be translated into a store expression."

Anybody can Help me what I do wrong?


You're trying to use SqlMethods.Like when from LINQ to Entities. As per the documentation:

This method is currently only supported in LINQ to SQL queries.

You haven't really explained why Contains/StartsWith/EndsWith don't work for you (and in my experience they do get translated into LIKE clauses).


You could write your query as:

var query = from c in _context.prgCity 
            where c.FullName.EndsWith("buda")
            select c.FullName + "|" + c.prgCountry.CountryName;


where c.FullName.Contains("buda")


Use Contains instead of SQLMethods.Like. It will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜