Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Like in Linq Query?

Tags:

linq

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

name like='apple';

thanks..

like image 895
Jitendra Jadav Avatar asked Jun 29 '10 09:06

Jitendra Jadav


2 Answers

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"));
like image 168
Jon Skeet Avatar answered Oct 23 '22 21:10

Jon Skeet


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

like image 28
ChrisF Avatar answered Oct 23 '22 21:10

ChrisF