48

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;
Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117
mweber
  • 493
  • 1
  • 4
  • 6

8 Answers8

46

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;
Kieron
  • 26,748
  • 16
  • 78
  • 122
46

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.

Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117
  • I didn't have time to monkey around with this : where SqlMethods.Like(s.Email, id + "%") that through an error (yes i have the using System.Data.Linq.SqlClient; , I changed to where s.Email.Contains(id) even though that is not the same, I can make it work with changing to EndsWith – Tom Stickel Oct 20 '15 at 17:29
  • 6
    For EF Core it's `Microsoft.EntityFrameworkCore.EF.Functions.Like(c.FullName, $"%{fullName}%")`. – Alternatex Aug 09 '18 at 12:02
  • I also thought there was something wrong with the SqlMethods.Like, but my .NET Framework just is 4.8 and needs to bee 4.8.1. – Bolle Jan 26 '23 at 18:44
18

Try like this

var results = db.costumers.Where(X=>X.FullName.Contains(FirstName)&&(X=>X.FullName.EndsWith(LastName))
                          .Select(X=>X);
Thorin Oakenshield
  • 14,232
  • 33
  • 106
  • 146
  • 4
    Why not using the '&&' operator, instead of the double Where clause? – Max Dec 04 '13 at 13:33
  • 1
    @max Why not 2 where clause? If each `where` clause are on its own line, it make the code more readable if there are many long conditions. – Phil1970 Aug 04 '16 at 16:28
  • 1
    This should be the answer. @Phil1970 Double Where would be a good choice if you have more than 3 conditions. It's my personal opinion though. – SouravOrii Jan 23 '22 at 12:14
6

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();
Adel Mourad
  • 1,351
  • 16
  • 13
  • 1
    not only EF6, required EF 6.2.0 https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dbfunctions.like?view=entity-framework-6.2.0 – Wangsu Apr 09 '20 at 13:44
  • 1
    Just wanted to say that .FirstOrDefault() may be extra in this example, since you may not be wanting only 1 result. – Literate Corvette Nov 16 '22 at 17:48
5
 where c.FullName.Contains("string")
Praveen Prasad
  • 31,561
  • 18
  • 73
  • 106
4
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.

Ali Haider
  • 467
  • 1
  • 6
  • 15
2

You can use contains:

string[] example = { "sample1", "sample2" };
var result = (from c in example where c.Contains("2") select c);
// returns only sample2
hwcverwe
  • 5,287
  • 7
  • 35
  • 63
0

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.