public Articles GetByName(string name, Categories category, Companies company)
{
var query = from article in session.Linq<Articles>()
where article.Name == name &&
article.Category == category &&
article.Company == company
select article;
return query.FirstOrDefault();
}
how can query be case insensitive. I can use toLower or toUpper but i want with OrdinalIgnoreCase. Is it possible?
Use String.Equals
with the appropriate parameters to make it case insensitive
mySource.Where(s => String.Equals(s, "Foo", StringComparison.CurrentCultureIgnoreCase));
Instead of ==
use the .Equals(name, StringComparison.OrdinalIgnoreCase)
method.
var query = from article in session.Linq<Articles>()
where article.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
article.Category.Equals(category) &&
article.Company.Equals(company)
select article;
return query.FirstOrDefault();
If this is a LINQ to SQL query against a database with a case-insensitive collation, then it already is case-insensitive. Remember that LINQ to SQL isn't actually executing your == call; it's looking at it as an expression and converting it to an equality operator in SQL.
If it's LINQ to Objects, then you can use String.Equals as the other posters have pointed out.
var query = from article in session.Linq<Articles>()
where string.Equals(article.Name,name, StringComparison.OrdinalIgnoreCase) &&
string.Equals(article.Category,category, StringComparison.OrdinalIgnoreCase) &&
string.Equals(article.Company,company, StringComparison.OrdinalIgnoreCase)
select article;
return query.FirstOrDefault();
It will also handle when Name,Category,Company is null
Use
String.Equals(article.Name, name, StringComparison.OrdinalIgnoreCase)
If you are using C# 6.0 you can define a short extension method to be used when constructing LINQ statements:
public static bool EqualsInsensitive(this string str, string value) => string.Equals(str, value, StringComparison.CurrentCultureIgnoreCase);
Usage:
query.Where(item => item.StringProperty.EqualsInsensitive(someStringValue));
For C# less than 6.0 it will look like this:
public static bool EqualsInsensitive(this string str, string value)
{
return string.Equals(str, value, StringComparison.CurrentCultureIgnoreCase);
}
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