EF错误:Too high level of nesting for select

Posted yuyoho

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF错误:Too high level of nesting for select相关的知识,希望对你有一定的参考价值。

问题描述:

平常在使用EF Linq 执行数据库查询时,错误的使用Any进行数据筛选导致的错误:Too high level of nesting for select,我们先来看看使用方法:

var officeIds = new List<int>{69,20,55,67,9,51,59,18,19,8,17,44,4,3,58,56,57,52,29,64,48,15,65,61,50,62,16,68,49,12,11,7,5};
var query = context.Office.Where(p=> officeIds.Any(o=> o == p.OfficeId));

  

监听数据库执行语句如下:

SELECT
`Extent1`.`OfficeID`,
`Extent1`.`DoctorPhone`,
`Extent1`.`Name`,
`Extent1`.`RegionID`,
`Extent1`.`PriceTier2`,
`Extent1`.`PriceTier3`,
`Extent1`.`PracticeManagementSoftware`,
`Extent1`.`ServerHost`,
`Extent1`.`ServerLogin`,
FROM `office` AS `Extent1`
WHERE EXISTS(SELECT
1 AS `C1`
FROM (SELECT `UnionAll31`.`C1` 
FROM (SELECT `UnionAll30`.`C1` 
FROM (SELECT `UnionAll29`.`C1` 
FROM (SELECT `UnionAll28`.`C1` 
FROM (SELECT `UnionAll27`.`C1` 
FROM (SELECT `UnionAll26`.`C1` 
FROM (SELECT `UnionAll25`.`C1` 
FROM (SELECT `UnionAll24`.`C1` 
FROM (SELECT `UnionAll23`.`C1` 
FROM (SELECT `UnionAll22`.`C1` 
FROM (SELECT `UnionAll21`.`C1` 
FROM (SELECT `UnionAll20`.`C1` 
FROM (SELECT `UnionAll19`.`C1` 
FROM (SELECT `UnionAll18`.`C1` 
FROM (SELECT `UnionAll17`.`C1` 
FROM (SELECT `UnionAll16`.`C1` 
FROM (SELECT `UnionAll15`.`C1` 
FROM (SELECT `UnionAll14`.`C1` 
FROM (SELECT `UnionAll13`.`C1` 
FROM (SELECT `UnionAll12`.`C1` 
FROM (SELECT `UnionAll11`.`C1` 
FROM (SELECT `UnionAll10`.`C1` 
FROM (SELECT `UnionAll9`.`C1` 
FROM (SELECT `UnionAll8`.`C1` 
FROM (SELECT `UnionAll7`.`C1` 
FROM (SELECT `UnionAll6`.`C1` 
FROM (SELECT `UnionAll5`.`C1` 
FROM (SELECT `UnionAll4`.`C1` 
FROM (SELECT `UnionAll3`.`C1` 
FROM (SELECT `UnionAll2`.`C1` 
FROM (SELECT `UnionAll1`.`C1` 
FROM (SELECT 69 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable1` UNION ALL SELECT 20 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable2`) AS `UnionAll1` UNION ALL SELECT 55 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable3`) AS `UnionAll2` UNION ALL SELECT 67 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable4`) AS `UnionAll3` UNION ALL SELECT 9 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable5`) AS `UnionAll4` UNION ALL SELECT 51 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable6`) AS `UnionAll5` UNION ALL SELECT 59 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable7`) AS `UnionAll6` UNION ALL SELECT 18 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable8`) AS `UnionAll7` UNION ALL SELECT 19 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable9`) AS `UnionAll8` UNION ALL SELECT 8 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable10`) AS `UnionAll9` UNION ALL SELECT 17 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable11`) AS `UnionAll10` UNION ALL SELECT 44 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable12`) AS `UnionAll11` UNION ALL SELECT 4 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable13`) AS `UnionAll12` UNION ALL SELECT 3 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable14`) AS `UnionAll13` UNION ALL SELECT 58 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable15`) AS `UnionAll14` UNION ALL SELECT 56 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable16`) AS `UnionAll15` UNION ALL SELECT 57 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable17`) AS `UnionAll16` UNION ALL SELECT 52 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable18`) AS `UnionAll17` UNION ALL SELECT 29 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable19`) AS `UnionAll18` UNION ALL SELECT 64 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable20`) AS `UnionAll19` UNION ALL SELECT 48 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable21`) AS `UnionAll20` UNION ALL SELECT 15 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable22`) AS `UnionAll21` UNION ALL SELECT 65 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable23`) AS `UnionAll22` UNION ALL SELECT 61 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable24`) AS `UnionAll23` UNION ALL SELECT 50 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable25`) AS `UnionAll24` UNION ALL SELECT 62 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable26`) AS `UnionAll25` UNION ALL SELECT 16 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable27`) AS `UnionAll26` UNION ALL SELECT 68 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable28`) AS `UnionAll27` UNION ALL SELECT 49 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable29`) AS `UnionAll28` UNION ALL SELECT 12 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable30`) AS `UnionAll29` UNION ALL SELECT 11 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable31`) AS `UnionAll30` UNION ALL SELECT 7 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable32`) AS `UnionAll31` UNION ALL SELECT 5 AS `C1` 
FROM (SELECT 1 AS `X`) AS `SingleRowTable33`) AS `UnionAll32`
WHERE (`UnionAll32`.`C1`) = `Extent1`.`OfficeID`)

  

解决方法:

将EF Linq Where查询中的Any()改成Contains()

var officeIds = new List<int>{69,20,55,67,9,51,59,18,19,8,17,44,4,3,58,56,57,52,29,64,48,15,65,61,50,62,16,68,49,12,11,7,5};
var query = context.Office.Where(p=> officeIds.Contains(p.OfficeId));

监听到的SQL语句如下:

SELECT
`Extent1`.`OfficeID`,
`Extent1`.`DoctorPhone`,
`Extent1`.`Name`,
`Extent1`.`RegionID`,
`Extent1`.`IsActive`,
`Extent1`.`IsDeleted`,
`Extent1`.`InsuranceOnly`,
`Extent1`.`MinimumFlatRate`,
FROM `office` AS `Extent1`
WHERE `Extent1`.`OfficeID` 
IN ( 69,20,55,67,9,51,59,18,19,8,17,44,4,3,58,56,57,52,29,64,48,15,65,61,50,62,16,68,49,12,11,7,5 )

 结语:

  EF虽然能够极大方便开发人员,但是,使用不好,也是会存在很多隐患。还是需要我们在开发中多总结,多思考。

以上是关于EF错误:Too high level of nesting for select的主要内容,如果未能解决你的问题,请参考以下文章

解决 ln -s 软链接产生的Too many levels of symbolic links错误

ffmpeg 错误 real-time buffer [USB2.0 Camera] [video input] too full or near too full (101% of size: 30

Too many levels of symbolic links 问题

hadoop.docker.up.problems: Too many levels of symbolic links

-bash: /usr/bin/mvn: Too many levels of symbolic links

报错“-bash: /usr/local/bin/node: Too many levels of symbolic links“