How can I do case insensitive string search with Linq and SQL Server?

主宰稳场 提交于 2019-12-08 07:01:02

问题


Here is my current code for searching tags:

    public JsonResult TagSearch(string term) {
        if (term == null || term == "")
            return Json("");

        var tags = (from t in _session.All<Tag>() where t.Name.Contains(term) select t.Name).Take(6).ToArray();

        return Json(tags);
    }

How could I do case insensitive string search instead?


回答1:


The Contains() method is converted to case-insensitive operation in SQL. I think the code I posted is case insensitive.




回答2:


Is changing the collation of the column out of the question?




回答3:


Use the ToLower method. Like this:

var tags = (from t in _session.All<Tag>() where t.Name.ToLower().Contains(term.ToLower()) select t.Name).Take(6).ToArray();


来源:https://stackoverflow.com/questions/3314130/how-can-i-do-case-insensitive-string-search-with-linq-and-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!