我在一个表中有超过 600,000 条记录和 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();
也许您应该尝试对所有可能的属性仅运行一次majorAgents.Any
查询。为此,您可以尝试连接firstName
、lastName
和addressProof
字段,并检查majorAgent
字符串是否存在于此串联字符串中的任何地方。
注意:我故意修改了与Address
相关的条件,因为它似乎与手头的任务无关。在原始查询中,它说:w.Address.Any(s=>majorAgents.Contains(s.addressProof)
这意味着"如果有任何地址证明,请检查主要代理",而检查是否addressProof of any Address has any of the majorAgents
会更明智。为此,我使用了w.Address.SelectMany(a => a.addressProof).ToArray()
和string.Join
,这将为我提供空格分隔的地址证明字符串,我可以在其中查找任何主要代理。如果这不是本意,请根据要求修改地址的该部分。
因此,要尝试的修改查询是:
var majorAgents = new[] { "iPhone", "Android", "iPad" };
List<Person> lstperson =_context.person.where
(w=>majorAgents.Any(x=>(w.personLastName + " " + w.personFirstName + " " +string.Join(" ",w.Address.SelectMany(a => a.addressProof).ToArray())).contains(x))
)
.select(s=> new Person{
s.personId,
s.perosnLastName,
s.personFirstName
}).ToList();
希望对您有所帮助。
执行包含将比创建"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();