为什么内存列表包含不区分大小写的代码?

时间:2018-07-27 16:49:28

标签: c# mysql linq .net-core

我有一个内存列表和来自MySQL的对象

requiredUserNames:调用 “其中users.Contains(us.Username)” ,为什么它不区分大小写包含用户是内存列表对象(普通c#对象)而第二个代码正常工作,因为C#区分大小写:

var users = (from u in _dbContext.MyUser
             select new 
             {
                 UserName = u.UserName
             }).ToList();

var requiredUserNames = (from us in _dbContext.NextUsers
                    // users is a memory list and not a database list why executed as case insensitive
                   where users.Contains(us.Username)
                   select new { Username = us.Username}).ToList();


var requiredUserNames02 = (from us in _dbContext.NextUsers
                        // here working normally as case sensitive
                     where users.FindIndex((usr) => usr.Username.Equals(us.Username)) > -1
                   select new { Username = us.Username }).ToList();

1 个答案:

答案 0 :(得分:1)

users是本地列表,这是正确的。但是_dbContext.NextUsers是远程的,您正在询问MySQL实例,例如:

select us.* from NextUsers us where us.UserName in ('theNameFromLocalList','...')

IOW是MySQL执行不敏感的搜索。如果您避免这种情况,那么这样做的成本会更高,但是:

var requiredUserNames = (from us in _dbContext.NextUsers.AsEnumerable()
                    // users is a memory list and not a database list why executed as case insensitive
                   where users.Contains(us.Username)
                   select new { Username = us.Username}).ToList();