C#

How to Use “Like” in C# LINQ

Utilizing the “Like” operator in C# LINQ (Language-Integrated Query) allows you to perform pattern matching and filter data based on specific patterns within strings. While LINQ does not directly support the “Like” operator like SQL does, you can achieve similar functionality using methods provided by LINQ. Let’s explore how to use “Like” in C# LINQ effectively.

Using “Contains” Method

One common approach to simulate the “Like” operator in LINQ is by using the Contains method, which checks if a string contains a specified substring.

var filteredItems = items.Where(item => item.Name.Contains("keyword"));

In this example, items is a collection of objects, and we filter them based on whether the Name property contains the specified keyword.

Using “StartsWith” and “EndsWith” Methods

You can also use the StartsWith and EndsWith methods to simulate the “Like” operator for pattern matching at the beginning or end of a string, respectively.

var filteredItems = items.Where(item => item.Name.StartsWith("prefix"));
var filteredItems = items.Where(item => item.Name.EndsWith("suffix"));

These examples demonstrate filtering items based on whether the Name property starts or ends with the specified prefix or suffix.

Using Regular Expressions

For more complex pattern matching, you can leverage regular expressions with LINQ to achieve advanced filtering based on intricate patterns within strings.

var filteredItems = items.Where(item => Regex.IsMatch(item.Name, @"pattern"));

In this example, we use a regular expression pattern to filter items based on custom criteria defined by the pattern.

By employing these techniques with LINQ methods, you can effectively perform pattern matching and filtering similar to the “Like” operator in C# LINQ.

Danilo Cavalcante

Working with web development since 2005, currently as a senior programmer analyst. Development, maintenance, and integration of systems in C#, ASP.Net, ASP.Net MVC, .Net Core, Web API, WebService, Integrations (SOAP and REST), Object-Oriented Programming, DDD, SQL, Git, and JavaScript

View Comments

  • Awesome blog you have here but I was wondering if you knew of any forums that cover the same topics discussed in this article?

    I'd really love to be a part of group where
    I can get opinions from other knowledgeable individuals that share the same interest.
    If you have any suggestions, please let me know. Thanks a lot!

Share
Published by
Danilo Cavalcante

Recent Posts

How to Split a String into an Array in C#

Splitting a string into an array of substrings is a common operation in C# programming,…

3 days ago

Starting the Docker Daemon: A Step-by-Step Guide

Starting the Docker daemon is the first step towards managing Docker containers and images on…

1 week ago

How to Serialize an Object in C# to JSON

Serializing an object to JSON (JavaScript Object Notation) format is a common task in C#…

1 week ago

How to Allow Docker Access Outside the Network

When running Docker containers, you may encounter scenarios where containers need to access resources outside…

2 weeks ago

How to Insert into Array in C#

Inserting elements into an array dynamically is a common operation in C# programming, especially when…

2 weeks ago

Can Docker Use GPU?

Utilizing GPUs (Graphics Processing Units) can significantly accelerate certain computational tasks, particularly those involving parallel…

2 weeks ago