Home > Backend Development > C++ > How Can I Perform Case-Insensitive String Comparisons Using LINQ's Contains Method?

How Can I Perform Case-Insensitive String Comparisons Using LINQ's Contains Method?

DDD
Release: 2025-01-05 17:35:43
Original
204 people have browsed it

How Can I Perform Case-Insensitive String Comparisons Using LINQ's Contains Method?

Case-Insensitive String Comparisons Using LINQ's Contains Method

When working with string comparisons in LINQ queries, it's essential to consider case sensitivity. By default, the Contains method is case-sensitive, meaning that "CaseSensitive" and "casesensitive" will not match.

Problem:

In the following LINQ query, we aim to retrieve FACILITY_ITEM entities based on a description filter. However, the query is currently case-sensitive and may not return the desired results.

public IQueryable<FACILITY_ITEM> GetFacilityItemRootByDescription(string description)
{
    return this.ObjectContext.FACILITY_ITEM.Where(fi => fi.DESCRIPTION.Contains(description));
}
Copy after login

Solution:

To make the comparison case-insensitive, we can utilize the ToLower() method before performing the Contains check. This converts both the description filter and the database values to lowercase, ensuring a case-insensitive comparison.

fi => fi.DESCRIPTION.ToLower().Contains(description.ToLower())
Copy after login

By using this updated expression, the query will return results where the facility item description matches the description filter, regardless of its case.

The above is the detailed content of How Can I Perform Case-Insensitive String Comparisons Using LINQ's Contains Method?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template