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

//...

}

}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...