开发者

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

开发者 https://www.devze.com 2023-01-08 17:43 出处:网络
Here is my current code for searching tags: public JsonResult TagSearch(string term) { if (term == null || term == \"\")

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 开发者_如何学JAVAinstead?


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消