linq case insensitive (without toUpper or toLower)

C#Linq

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

Use String.Equals with the appropriate parameters to make it case insensitive

mySource.Where(s => String.Equals(s, "Foo", StringComparison.CurrentCultureIgnoreCase));

Solution 2 - C#

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();

Solution 3 - C#

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.

Solution 4 - C#

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

Solution 5 - C#

Use

String.Equals(article.Name, name, StringComparison.OrdinalIgnoreCase)

Solution 6 - C#

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

Solution 7 - C#

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

Solution 8 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsenzacionaleView Question on Stackoverflow
Solution 1 - C#Adam RackisView Answer on Stackoverflow
Solution 2 - C#EdgarView Answer on Stackoverflow
Solution 3 - C#John BledsoeView Answer on Stackoverflow
Solution 4 - C#StecyaView Answer on Stackoverflow
Solution 5 - C#Daniel HilgarthView Answer on Stackoverflow
Solution 6 - C#Alexei - check CodidactView Answer on Stackoverflow
Solution 7 - C#ChadView Answer on Stackoverflow
Solution 8 - C#StefanGView Answer on Stackoverflow