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!

Recent Posts

Encapsulation and Abstraction in C#

Encapsulation and abstraction are two pillars of object-oriented programming (OOP) that play a vital role…

4 weeks ago

Polymorphism in C#: Object-Oriented Programming

Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects to take on…

4 weeks ago

Understanding Inheritance in C#

Inheritance is a cornerstone of object-oriented programming (OOP) and one of its most powerful features.…

4 weeks ago

Classes and Objects in C#: Object-Oriented Programming

In the world of C# and object-oriented programming (OOP), classes and objects form the backbone…

1 month ago

Collections and LINQ Queries in C#

In modern C# programming, working with data collections is a common task. Understanding how to…

1 month ago

Exception Handling in C#: try-catch, finally, and Custom Exceptions

Exception handling is a critical part of writing robust and maintainable C# applications. It allows…

1 month ago