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);
}
Change it to
public Articles GetByName(string name, Categories category, Companies company)
{
var query = from article in session.Linq<Articles>()
where string.Equals(article.Name, StringComparison.CurrentCultureIgnoreCase) == name &&
string.Equals(article.Category, StringComparison.CurrentCultureIgnoreCase == category &&
string.Equals(article.Company, StringComparison.CurrentCultureIgnoreCase == company
select article;
return query.FirstOrDefault();
}
Use string.Equals(name, article.Name, StringComparison.OrdinalIgnoreCase) when you are sure that your database supports it.
E.g. SQLite with a collate of NOCASE will ignore the option. Oracle uses session settings NLS_COMP, NLS_SORT that will be taken.
Else use ToLower() or ToUpper() when you have no culture issues.
精彩评论