Mongo DB query case sensitive [duplicate] - c#

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"
}));

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;

Use a Like expression in Lambda [duplicate]

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.

How to compare case insensitive string using FluentAssertions? C# [duplicate]

This question already has answers here:
Can Fluent Assertions use a string-insensitive comparison for IEnumerable<string>?
(4 answers)
Closed 5 years ago.
How can I easy compare string case insensitive using FluentAssertions?
Something like:
symbol.Should().Be(expectedSymbol, StringComparison.InvariantCultureIgnoreCase);
Edit: Regarding possible duplicate and code:
symbol.Should().BeEquivalentTo(expectedSymbol);
it is comparing using CurrentCulture. And it will brake in situation like Turkish culture. Where
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR", false);
string upper = "in".ToUpper(); // upper == "İN"
"in".Should().BeEquivalentTo("In"); // It will fail
so the part "StringComparison.InvariantCultureIgnoreCase" is crucial here.
You can use
symbol.ToLower().Should().Be(expectedSymbol.ToLower());
OR
Instead of Be use BeEquivalentTo
symbol.Should().BeEquivalentTo(expectedSymbol);
BeEquivalentTo metadata states
Asserts that a string is exactly the same as another string, including any leading or trailing whitespace, with the exception of the casing.

LINQ 2 SQL Using Contains [duplicate]

This question already has answers here:
Using contains() in LINQ to SQL
(7 answers)
Closed 8 years ago.
I am trying to convert the following SQL statement to a Link2SQL statement.
SELECT * FROM Global.CustomData
WHERE CustomDataSource LIKE '%Plugin%'
I have converted it to this statement
var query =
from item in db.CustomDatas
where item.CustomDataSource.Contains(dataSource)
select item;
And have tried setting dataSource to the following: "Plugin", "%Plugin%", "/Plugin/" and "%/Plugin%/". These I have taken from other examples. Unfortunately, although the TSQL statement does return a value, I cannot get the Linq2Sql statement to return anything. Could someone tell me what I am doing wrong?
You should pass "Plugin", the only thing I can think of is the case sensitivity. Try something like this:
where item.CustomDataSource.ToLower().Contains(dataSource.ToLower())

C# Case insensitive string comparison [duplicate]

This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
How to ignore the case sensitivity in List<string>
(12 answers)
Closed 8 years ago.
I have a list that holds a few strings(names). For this example.
It will hold:
TeSt1
TeSt2
TeSt3
And I'm trying to check if that list has one of those. And I'm doing this like this at the moment:
if (list.Contains(test2))
{
}
But I need it to be case insensitive.. But how can I do that? in an if statement.
The Contains method has an overload that accepts an IEqualityComparer. You can give it one by doing the following:
if (list.Contains(test2, StringComparer.OrdinalIgnoreCase))
{
// do something
}
IndexOf has a parameter for case insensitive search
culture.CompareInfo.IndexOf(toSearch, word, CompareOptions.IgnoreCase)
where culture is the instance of CultureInfo describing the language that the text is written in.
You can loop through the list and see if it each list entry matches the search.
Make your list lower case......and
if (list.Contains(test2.ToLower()))
{
}

Categories