Home > Backend Development > C++ > How Can I Perform a Case-Insensitive Contains Query in LINQ?

How Can I Perform a Case-Insensitive Contains Query in LINQ?

DDD
Release: 2025-01-03 02:03:40
Original
913 people have browsed it

How Can I Perform a Case-Insensitive Contains Query in LINQ?

Case-Insensitive Contains Query in LINQ

When performing a contains operation in LINQ queries, the default behavior is case-sensitive. However, there are situations when you may want to search for a substring in a case-insensitive manner.

One example is the following code, which searches for a facility item based on a description:

public IQueryable<FACILITY_ITEM> GetFacilityItemRootByDescription(string description)<br>{</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">return this.ObjectContext.FACILITY_ITEM.Where(fi => fi.DESCRIPTION.Contains(description));
Copy after login

}

In this code, if the description parameter is uppercase, it will only find items that have descriptions that are also uppercase. To make this query case-insensitive, you can use the following approach:

fi => fi.DESCRIPTION.ToLower().Contains(description.ToLower())<br>

This code converts both the description parameter and the DESCRIPTION property of each FACILITY_ITEM to lowercase before performing the contains operation. This ensures that the query will match items regardless of the case of the input.

The above is the detailed content of How Can I Perform a Case-Insensitive Contains Query in LINQ?. For more information, please follow other related articles on the PHP Chinese website!

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template