在Linq中使用多个包含花费时间来加载性能问题

Gau*_*san 5 c# linq

我在一个表中有60万条记录和20列。

我想使用LINQ查询,并使用充当“ LIKE”的函数;所以我用了Contains。花费时间(或)会引发超时过期异常。

那么有人可以建议如何解决这个问题吗?我需要比较4列以上。

var majorAgents = new[] { "iPhone", "Android", "iPad" };


List<Person> lstperson =_context.person.where 
                      (w=>majorAgents.Any(x=>w.personLastName.contains(x))
                      || majorAgents.Any(x=>w.personFirstName.contains(x))
                      ||w.Address.Any(s=>majorAgents.Contains(s.addressProof)))//Address table referenced as a list 
                        .select(s=> new Person{
                             s.personId,
                             s.perosnLastName,
                             s.personFirstName
                    }).ToList();
Run Code Online (Sandbox Code Playgroud)

Jon*_*ago -1

执行 contains 比执行“any”更具性能。进行以下测试:

var majorAgents = new[] { "iPhone", "Android", "iPad" };

List<Person> lstperson=_context.person.where 
     (c=> majorAgents.contains(c.personLastName) || majorAgents.contains(c.personFirstName))
    .select(s=> new Person{
             s.PersonId,
             s.perosnLastName,
             s.personFirstName
}).ToList();
Run Code Online (Sandbox Code Playgroud)