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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...