0

Experts. I want to search the 'Name' field in my IList...but it is always case sensitive.How to search without case sensitive? Below are my codes:

Model:

public IList<Student> Search(IList<Student> list, string keyword)
{
    return list.Where(e => e.Name.Contains(keyword)).ToList();
}

Class:

public class Student
{
    public string Name {get;set;}
    public string MatricNo {get;set;}
    public string Gender {get;set;}
}

Controller:

IList<Student> list = new List<Student>();

Student students1 = new Student();
students1.Name = "Mike";
students1.MatricNo = "12345";
students1.Gender = "Male";
list.Add(students1);

Student students2 = new Student();
students2.Name = "Steve";
students2.MatricNo = "12345";
students2.Gender = "Male";
list.Add(students2);

Student students3 = new Student();
students3.Name = "Jane";
students3.MatricNo = "12345";
students3.Gender = "Male";
list.Add(students3);

string keyword = "mik"; //Example of search keyword
list = _searchModel.Search(list, keyword);

I want the list the return student named Mike, but it doesn't return. Instead it will return only if the keyword = "Mik". How to do the searching without case sensitive when keyword = "mik"? Please note that keyword can be a substring of 'Name' of student.

erntay2
  • 140
  • 3
  • 16
  • I also tried list.Where(e => e.Name.ToLower().Contains(keyword.ToLower())) but still case sensitive... – erntay2 Feb 02 '16 at 03:31
  • You may look at this question https://stackoverflow.com/questions/18378448/best-way-to-compare-two-strings-and-their-upper-and-lower-case-signs . – Kote Feb 02 '16 at 03:35
  • sorry Kote, i have changed a bit on my question...keyword can be substring...is that working? – erntay2 Feb 02 '16 at 03:41
  • @erntay2 Are you sure your ToLower comparison ( on both ) did not work ? – Shyju Feb 02 '16 at 03:43
  • It looks like you code is changing the list because list is both an output and an input in your Search. Your search using ToLower() should work unless you have some non Ascii character (unicode or non english characters). – jdweng Feb 02 '16 at 03:43
  • 1
    @erntay2 sry, wrong link. Look at this one: https://stackoverflow.com/questions/444798/case-insensitive-containsstring – Kote Feb 02 '16 at 03:51

1 Answers1

2

I copied and pasted your example using .ToLower() and it works just fine for me in LinqPad. I changed the search keyword to use a few more variants just to show the results more clearly.

My code exactly:

void Main()
{
    IList<Student> list = new List<Student>();

    Student students1 = new Student();
    students1.Name = "Mike";
    students1.MatricNo = "12345";
    students1.Gender = "Male";
    list.Add(students1);

    Student students2 = new Student();
    students2.Name = "Steve";
    students2.MatricNo = "12345";
    students2.Gender = "Male";
    list.Add(students2);

    Student students3 = new Student();
    students3.Name = "Jane";
    students3.MatricNo = "12345";
    students3.Gender = "Male";
    list.Add(students3);

    var test1 = Search(list, "mik"); //returns Mike
    var test2 = Search(list, "MIK"); //returns Mike
    var test3 = Search(list, "iKe"); //returns Mike
    //all three are the same
}

public IList<Student> Search(IList<Student> list, string keyword)
{
    return list.Where(e => e.Name.ToLower().Contains(keyword.ToLower())).ToList();
}

public class Student
{
    public string Name {get;set;}
    public string MatricNo {get;set;}
    public string Gender {get;set;}
}
Matt Wanchap
  • 841
  • 8
  • 20
  • Does it returns all the 3 students records? – erntay2 Feb 02 '16 at 03:42
  • There, does that help? I'm thinking the .ToLower() might not be your problem here. How are you testing the output? Also, jdweng made a good point that you're reassigning the "list" variable. – Matt Wanchap Feb 02 '16 at 03:52