Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq case insensitive (without toUpper or toLower)

Tags:

c#

linq

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?

like image 461
senzacionale Avatar asked Mar 15 '11 13:03

senzacionale


6 Answers

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

mySource.Where(s => String.Equals(s, "Foo", StringComparison.CurrentCultureIgnoreCase));
like image 107
Adam Rackis Avatar answered Oct 01 '22 18:10

Adam Rackis


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();
like image 35
Edgar Avatar answered Oct 01 '22 18:10

Edgar


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.

like image 27
John Bledsoe Avatar answered Sep 30 '22 18:09

John Bledsoe


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

like image 21
Stecya Avatar answered Sep 30 '22 18:09

Stecya


Use

String.Equals(article.Name, name, StringComparison.OrdinalIgnoreCase)
like image 44
Daniel Hilgarth Avatar answered Oct 01 '22 18:10

Daniel Hilgarth


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);
}
like image 40
Alexei - check Codidact Avatar answered Oct 01 '22 18:10

Alexei - check Codidact