Jitendra Jadav
Jitendra Jadav

Reputation: 603

How do I use Like in Linq Query?

How I can use Like query in LINQ .... in sql for eg..

name like='apple';

thanks..

Upvotes: 7

Views: 2662

Answers (4)

Marco Lépiz
Marco Lépiz

Reputation: 1

I use item.Contains("criteria"), but, it works efficiently only if you convert to lower both, criteria and item like this:

 string criteria = txtSearchItemCriteria.Text.ToLower();

 IEnumerable<Item> result = items.Where(x => x.Name.ToLower().Contains(criteria));

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503669

Use normal .NET methods. For example:

var query = from person in people
            where person.Name.StartsWith("apple") // equivalent to LIKE 'apple%'
            select person;

(Or EndsWith, or Contains.) LINQ to SQL will translate these into the appropriate SQL.

This will work in dot notation as well - there's nothing magic about query expressions:

// Will find New York
var query = cities.Where(city => city.Name.EndsWith("York"));

Upvotes: 11

ChrisF
ChrisF

Reputation: 137188

You need to use StartsWith, Contains or EndsWith depending on where your string can appear. For example:

var query = from c in ctx.Customers
            where c.City.StartsWith("Lo")
            select c;

will find all cities that start with "Lo" (e.g. London).

var query = from c in ctx.Customers
            where c.City.Contains("York")
            select c;

will find all cities that contain "York" (e.g. New York, Yorktown)

Source

Upvotes: 5

Geoff Appleford
Geoff Appleford

Reputation: 18832

name.contains("apple");

Upvotes: 0

Related Questions