LIKE operator in LINQ

by Ginktage | Friday, Jan 11, 2013 | 314 views

Does the LINQ(Language Integrated Query) have the LIKE operator like the one we have in the SQL Server or any other databases ?

I have been searching for similar function in LINQ and below are some of the ways which you could bring in the functionality of the LIKE operator in LINQ.

LIKE operator in LINQ

1. Using the Contains method.

List<Movie> movies = new Movies();

IQueryable<Movie> moviesLinq1 = (from m in movies

orderby m.Actor , m.MovieName

where m.Actor.Contains("vijay")

select m).AsQueryable<Movie>();

2. Using the StartsWith method

List<Movie> movies = new Movies();

IQueryable<Movie> moviesLinq1 = (from m in movies

orderby m.Actor , m.MovieName

where m.Actor.StartsWith("vijay")

select m).AsQueryable<Movie>();

3. Using the EndsWith method

List<Movie> movies = new Movies();

IQueryable<Movie> moviesLinq1 = (from m in movies

orderby m.Actor , m.MovieName

where m.Actor.EndsWith("ay")

select m).AsQueryable<Movie>();

4. Using System.Data.Linq.SqlClient.SqlMethods.Like

If you are using LINQ to SQL , you can use the System.Data.Linq.SqlClient.SqlMethods.Like() method.

5. Create an LINQ extension method and use the regular expression

public static class LINQExtensions

{

public static bool Like(this string DataToSearch, string regexString)

{

//Regex Logic goes here

//...

}

}
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
Like it? Share it!

Leave A Response