Linq query not behaving as expected

前端 未结 3 1881
谎友^
谎友^ 2021-01-17 16:26

I have a very simple linq query which is as following:

var result = (from r in employeeRepo.GetAll()
              where r.EmployeeName.Contains(searchString         


        
3条回答
  •  野的像风
    2021-01-17 16:55

    SQL Server 2012 (SQL Server) is installed by default with case insensitive collation. If you need to retrieve records from the database using case sensitivity (because you have "several" records) you need to change the collation (take care because if you change DBMS collation you change also master database collation so also tables and field names become case sensitive).
    If you don't need to avoid to retrieve all the records from the DBMS you can just filter records after you retrieve them, i.e.

    var result = (from r in employeeRepo.GetAll()
              where r.EmployeeName.Contains(searchString) 
                    || r.SAMAccountName.Contains(searchString)
              orderby r.EmployeeName
              select new SelectListItem 
              { 
                  Text = r.EmployeeName, 
                  Value = r.EmployeeName 
              })
              .ToList()  // Materialize records and apply case sensitive filter
              .Where(r.EmployeeName.Contains(searchString) 
                    || r.SAMAccountName.Contains(searchString));
    

提交回复
热议问题