Use a Like expression in Lambda [duplicate] - c#

This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
Closed 4 years ago.
I have a Lambda expression that search the column after submitting a form.
It does work when I submit it but its doesn't search the right way I would like to search.
I would like to make it work the same way it search in SQL like statement.
select * FROM tableSearch where subject like '%f5%'
This way even if 'F' is capital it still finds it.
Can this be possible using Lambda expression.
With the below code it only finds it if 'F' is not capital unless i enter 'F5' in subject.
if (!string.IsNullOrEmpty(searchControl.subject))
{
searchList = searchList.Where(x => x.subject.Contains(searchControl.subject)).ToList();
}

why not make them both ToLower:
searchList.Where(x => x.subject.ToLower().Contains(searchControl.subject.ToLower()))
or:
searchList.Where(x => x.subject.IndexOf(searchControl.subject, StringComparison.OrdinalIgnoreCase) >= 0)

You need to use StringComparison as below:
searchList = searchList.Where(x => x.subject.Contains(searchControl.subject,StringComparison.CurrentCultureIgnoreCase)).ToList();
By using that you ignore case sensitivity.

Related

Wrong case sensitivity using linq [duplicate]

This question already has answers here:
LINQ to Entities case sensitive comparison
(8 answers)
Write a search query which is case insensitive in EF Core?
(2 answers)
Closed 1 year ago.
Please help with the following result. I am using linq to perform a query. I know the String.Equals(string) is case sensitive, and I have tested it. However, when I am applying String.Equals functions in the linq statement below, the result is case insensitive. And I can not find what went wrong.
In the result below, I am searching for "qiao", however, "Qiao" was returned as a valid result. And it is not expected. Please help.
for case-sensitive result you can try == operator instead of .Equals() method -
var query = from c in db.Contacts where c.Name == "qiao" select c;
You can use ToUpperCase() or ToLowerCase to solve case sensitivity problem.
Example-
var query = from c in db.Contacts where c.Name.ToUpperCase().Equals("QIAO")
select c;

How to group by ToLower()? [duplicate]

This question already has an answer here:
Return the name of a function in lowercase
(1 answer)
Closed 3 years ago.
In my project I read a csv file. In this csv file there are different functions and their duration. Now I want to group my result by the function name but sometimes the name of the function is the same but the letters are sometimes upper and lower case like: examplevalue, EXampleValue.
In this example this would be 2 different functions.
To solve this problem I want to convert these functions into lower case. I tried like this but it is not working:
var descItemsTemp = db.ChartDatas
.GroupBy(x => new { x.Function.ToLower() });
You can pass a comparer to GroupBy, in this case you can use an existing:
.GroupBy(x =>x.Function, StringComparer.OrdinalIgnoreCase);

How to apply LIKE in Entity Framework [duplicate]

This question already has answers here:
Like in Lambda Expression and LINQ
(6 answers)
Closed 5 years ago.
I'm trying to filter out results based on an input string, and I need to use SQL LIKE operation for that and not a normal comparison. Couldn't find the solution online(probably didn't use the correct search words) :
return _context.Cities.Where(t => t.Name == cityName);
And I need it to do WHERE t.name LIKE '%CityName%' . How do I simulate it here ?
_context.Cities.Where(t=> t.Name.Contains(cityName));

Mongo DB query case sensitive [duplicate]

This question already has answers here:
MongoDB: Is it possible to make a case-insensitive query?
(27 answers)
Closed 8 years ago.
How do I make this case insensitive? At the moment it works but only when the case matches.
var query = Query.Matches("Name", searchString);
You can store the names in all uppercase or lowercase, and convert your search query to the correct case. If you would like to persist the original casing, you can create an additional field for searching, in an all upper/lower case. Then convert your query to all upper/lower case, and query on the upper/lower case version of the field. This would perform much better than using a regex.
For example:
var query = Query.Matches("Name_Upper", searchString.ToUpper());
You can do this using regexes. The Mongo query would look like this:
find({"Name": {"$regex": searchString, "$options": "i"}})
In C# you would write it something like this:
Query.Matches("Name", new BsonDocument(new Dictionary<string, object> {
"$regex": searchString,
"$options": "i"
}));

Find string in text with linq regardless of case or culture [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ignoring accented letters in string comparison
I have a search textbox that searches the given text in all the news from the database. So I have this:
List<NewsTranslation> newsTranslations = GetByLanguage(GlobalBL.CultureLanguage);
return newsTranslations.Where(
e =>
e.NewsContent.Contains(searchText) || e.NewsDescription.Contains(searchText) ||
e.NewsTitle.Contains(searchText)).ToList();
which works fine, but I would need it not to consider case or accents on letters.
Thanks
This comparator:
string.Compare(searchText, e, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
will do the trick. You can include this in your LINQ query where it will of course return 0 when the two arguments are equivalent.

Categories