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;
Related
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.
This question already has answers here:
How to perform Dynamic Join using LINQ
(1 answer)
Linq Join with Dynamic Expression
(1 answer)
dynamic join in linq 0 c#
(2 answers)
Closed 5 years ago.
I stuck to find duplicates from 2 datatables where the condition is dynamic.
Let's say TABLEA and TABLEB has same schema
and comparison columns could be anything.
Columns could be more than one.
how can i build a dynamic condition to remove duplicate from TABLEA.
here i tried this query
e.g. i have column name with comma seperated
dynamiccolumnA, dynamiccolumnB, dynamiccolumnC
var matched = from table1 in dt1.AsEnumerable()
join table2 in dt2.AsEnumerable() on
table1.Field<object>(dynamiccolumn) equals table2.Field<object>(dynamiccolumn)
where table1.Field<object>(dynamiccolumn) != table2.Field<object>(dynamiccolumn)
select table1;
" where table1.Field(dynamiccolumn) != table2.Field(dynamiccolumn)" this statment could be for more than one column.
Could anybody gives me some pointer regarding this.
Thanks in advance
Looking at your problem, you need a query which can be customized without having the source code recompiled. I would suggest you to use expression trees. They are tough to understand and can bring complexity but its well worth the price.
Take a look at this article
Microsoft Article on Expression Trees
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())
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"
}));
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
LINQ Between Operator
Dear All,
Hi,
I need to write this query in LINQ C#. can anyone help me?
Select *
From Mytable
where MyText BETWEEN 'john' AND 'Pear'
I believe this query should work:
var results = yourTable.Where(x => x.Text.CompareTo("john") > 0 &&
x.Text.CompareTo("Pear") < 0);
This assumes that you want to compare the text in each row of the table, and not some pre-dfined string.
Here is how you can do it with ObjectQuery
MytableSet.Where("it.Name between #start and #end", new ObjectParameter("start", "john"), new ObjectParameter("end", "Pear"))
EDIT:
Forget to mention that this statement is specific to Entity Framework not LINQ2SQL.