jeudi 29 octobre 2015

Compare strings ignoring case

I am trying to find entities (Tags) in the database via their Name property, where it is important to ignore the case (When database is searched and the Database contains a Tag with the name Database, that one should be returned). Right now, my code looks like this:

public IEnumerable<db.Tag> FindByNames(IEnumerable<string> tagNames)
{
    return this.DatabaseContext.Tags
        .Where(tag => tagNames.Contains(tag.Name));
}

Obviously, this one is case sensitive. I have tried to provide StringComparer.OrdinalIgnoreCase as comparer to the Contains method, but got a warning during execution that the expression could not be translated and would be executed in code rather than in the database (I don't remember the exact message, I will edit as soon as I am back on my development machine). I can live with that if I have to, but it would be nice to know how to let the database do the work in this case. Is it possible?

Aucun commentaire:

Enregistrer un commentaire