צח חורי
צח חורי

Reputation: 401

Find records that contains at least one word of a string

I have the table Doctors

Doctors:
------------
ID
FullName

The user inserts a string that can contain many words like : "Leon Simmons" and i want to get all the the doctors that has either "Leon" or "Simmons" in their names. The problem is that i don't know how many words will the user insert. How can I do such thing with linq?

Upvotes: 0

Views: 201

Answers (3)

Amir Molaei
Amir Molaei

Reputation: 3810

One possible solution using linq method syntax:

    dbContext.Doctors
     .Where(d => d.FullName.Contains("Leon") || d.FullName.Contains("Simmons"))
     .Count();

Upvotes: 1

Matt.G
Matt.G

Reputation: 3609

Try:

IQueryable<Doctor> doctorQuery = dbContext.Doctors;
foreach(var word in searchWords)
    doctorQuery = doctorQuery.Where(x => x.FullName.ToLower().Contains(word.ToLower());
return await doctorQuery.ToListAsync();

Upvotes: 1

hmiedema9
hmiedema9

Reputation: 988

You can use SqlMethods.Like().

An example of the usage:

var results =
        from d in doctors
        where SqlMethods.Like(u.FullName, "%Leon Simmons%")
        select d;

Upvotes: 1

Related Questions