开发者

How to do a LIKE query with linq? [duplicate]

This question already has answers here: LINQ version of SQL's LIKE statement (4 answers) 开发者_运维百科 Closed 1 year ago.

How can i perform an LIKE query within Linq?

I have the following query i would like to execute.

var results = from c in db.costumers
              where c.FullName LIKE "%"+FirstName+"%,"+LastName
              select c;


You could use SqlMethods.Like(matchExpression,pattern)

var results = from c in db.costumers
              where SqlMethods.Like(c.FullName, "%"+FirstName+"%,"+LastName)
              select c;

The use of this method outside of LINQ to SQL will always throw a NotSupportedException exception.


Try using string.Contains () combined with EndsWith.

var results = from c in db.Customers
              where c.FullName.Contains (FirstName) && c.FullName.EndsWith (LastName)
              select c;


Try like this

var results = db.costumers.Where(X=>X.FullName.Contains(FirstName)&&(X=>X.FullName.EndsWith(LastName))
                          .Select(X=>X);


2019 is here:

Requires EF6

using System.Data.Entity;
string searchStr ="bla bla bla";
var result = _dbContext.SomeTable.Where(x=> DbFunctions.Like(x.NameAr, string.Format("%{0}%", searchStr ))).FirstOrDefault();


String [] obj = (from c in db.Contacts
                           where c.FirstName.StartsWith(prefixText)
                           select c.FirstName).ToArray();
            return obj;

StartsWith() and EndsWith() can help you a lot here. If you want to find data in between the field, then Contains() can be used.


 where c.FullName.Contains("string")


You can use contains:

string[] example = { "sample1", "sample2" };
var result = (from c in example where c.Contains("2") select c);
// returns only sample2


var StudentList = dbContext.Students.SqlQuery("Select * from Students where Email like '%gmail%'").ToList<Student>();

User can use this of like query in Linq and fill the student model.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜