i am trying to query my resultset like this in linq to entities;
var categoriesList = _catRepo.GetAllCategories();
filteredCategories = categoriesList.Where(c=> c.CategoryName.Contains("for"));
However, i don't get any result becuase the CategoryName
is For(Upper Case)
in the database. I have also checked the sql server collation and it is set to _CI_AS. I have no idea how to use contains to filter case insensitive string?
I want basically if someone type like;
filteredCategories = categoriesList.Where(c=> c.CategoryName.Contains("for"));
OR
filteredCategories = categoriesList.Where(c=> c.CategoryName.Contains("For"));
The result should be the same
LINQ has no concept of case sensitivity, it only cares about boolean evaluation. So if you want to ignore case, you should do something like: query = query.
We need to set the NLS_COMP parameter to LINGUISTIC, which will tell the database to use NLS_SORT for string comparisons. Then we will set NLS_SORT to a case insensitive setting, like BINARY_CI. By default, NLS_COMP is set to BINARY.
By default, searches are case-insensitive. You can make your search case-sensitive by using the case filter. For example, the following search returns only results that match the term HelloWorld . It excludes results where the case doesn't match, such as helloWorld or helloworld . case:yes HelloWorld.
The Linq Contains Method in C# is used to check whether a sequence or collection (i.e. data source) contains a specified element or not. If the data source contains the specified element, then it returns true else return false.
Try this
filteredCategories = categoriesList.Where(c=>
c.CategoryName.IndexOf("for", StringComparison.OrdinalIgnoreCase) >= 0)
Contains method works as shown below
public bool Contains(string value)
{
return this.IndexOf(value, StringComparison.Ordinal) >= 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With