C#MVC API URL字符串值删除区分大小写

时间:2016-04-23 06:20:01

标签: c# asp.net-mvc asp.net-mvc-4

我有C#MVC API URL localhost / api / APIValues?Name = Nick。所有工作但唯一的问题是当我输入Name = nick时它不会显示结果。因为我的数据库表名字段存储了尼克。我的数据库表名称字段也有一些数据示例Nick,ANN,tristan,Abbott,BUD。如何删除字符串(Name)区分大小写的MVC API值? 例如,我如何设置localhost / api / APIValues的工作方式?Name = Nick和localhost / api / APIValues?Name = nick。

这是我的C#代码。

public IEnumerable<NameDTO> Get(string Name = "")
{
var nameList = (from o in db.People.AsEnumerable()
                       where  o.name == Name                       
                       join s in db.Employee on
                        o.empID equals s.empID
                     
                        select new 
                        {
                            
                           s.empID,
                            o.Id
                        }).ToList();
}

我的最终出局应该起到两个名字“尼克或尼克”

localhost/api/APIValues?Name=Nick
localhost/api/APIValues?Name=nick

4 个答案:

答案 0 :(得分:1)

您可以将等于StringComparison

public IEnumerable<NameDTO> Get(string Name = "")
{
var nameList = (from o in db.People.AsEnumerable()
                       where o.name.Equals(Name, StringComparison.OrdinalIgnoreCase)      
                       join s in db.Employee on
                        o.empID equals s.empID

                        select new 
                        {

                           s.empID,
                            o.Id
                        }).ToList();
}    

答案 1 :(得分:0)

尽可能简单。每当我的查询不能在一行中工作时,我倾向于将其分解为几个组件。尽管写一个班轮,但感到高兴。

var nameList= db.People.AsEnumerable();
            People people = new People();
            foreach (var x in nameList)
            {
                var result = x.name.ToLower() == Name.ToLower();
                if (result)
                {
                    people = x;
                }
            }

var Employee =  db.Employee.FirstOrDefault(e => e.EmpId == people.EmpId);

NameDTO nameDTO = new NameDTO()
{
 EmpId = Employee.EmpId,
 Id = People.Id
};

答案 2 :(得分:0)

SQL不区分大小写。只要您使用将代码转换为SQL(例如EF)的库,这不应该是一个问题。

var nameList = (from o in db.People
                       where  o.name == Name                       
                       join s in db.Employee on
                        o.empID equals s.empID
                        select new 
                        {                            
                           s.empID,
                            o.Id
                        }).ToList();

问题是您正在使用AsEnumerable()实际执行查询,然后比较内存中的对象而不是DB中的比较。在SQL事件探查器中观察它,您将看到差异。

答案 3 :(得分:0)

试试这个,我想它可以帮到你:

    // Use if you want same records  with name you provide

    public List<NameDTO> Get(string Name = "")
    {
        var nameList = (from o in db.People.AsEnumerable()
                        where o.name.Trim().ToLower() == Name.Trim().ToLower()
                        join s in db.Employee on
                         o.empID equals s.empID

                        select new NameDTO()
                        {
                          EmpId =  s.empID,
                           Id = o.Id
                        }).ToList();
    }

    //use this if you want similar records from database

    public IEnumerable<NameDTO> Get(string Name = "")
    {
        var nameList = (from o in db.People.AsEnumerable()
                        where o.name.Trim().ToLower().Contains(Name.Trim().ToLower())
                        join s in db.Employee on
                         o.empID equals s.empID
                       select new NameDTO()
                        {
                          EmpId =  s.empID,
                           Id = o.Id
                        }).ToList();
    }

}