LIKE operator in LINQ

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.

Leave a Reply


x

Interesting BlogPosts to read

How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...
How to convert Array to List in C#?
Want to convert array of strings to List of strings in C# ? . Below is a sample code to illustrate t...