asp.net - SQL 语句的某些部分嵌套得太深。重写查询或将其分解为更小的查询

标签 asp.net asp.net-mvc entity-framework linq-to-entities

我的 ASP.NET MVC Web 应用程序中有以下方法,并且我使用 Entity Framework 作为数据访问层:-

        public IEnumerable<AaaUserContactInfo> getcontactinfo(long[] id)
        {
var organizationsiteids = (from accountsitemapping in entities.AccountSiteMappings
where id.Any(accountid => accountsitemapping.ACCOUNTID == accountid)
select accountsitemapping.SITEID).ToList();

var usersdepts = from userdept in entities.UserDepartments
join deptdefinition in entities.DepartmentDefinitions on userdept.DEPTID equals deptdefinition.DEPTID

where organizationsiteids.Any(accountid => deptdefinition.SITEID == accountid)

var contactsinfos = from contactinfo in entities.AaaUserContactInfoes 
                                join userdept in usersdepts on  contactinfo.USER_ID equals userdept.USERID

                                 select contactinfo;

            return contactsinfos;

但是如果记录数量很大,那么我会收到以下错误:-

Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SqlException (0x80131904): Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) +388<br/> System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +688<br/> System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4403<br/> System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +82<br/> System.Data.SqlClient.SqlDataReader.get_MetaData() +135<br/> System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6665229<br/> System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +6667096<br/> System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +577
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +107
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +288
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +180
System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +689

如果返回的记录数很少,那么代码可以正常工作,那么可能是什么问题?

最佳答案

根据上面评论中提到的重复问题的答案,请尝试将以下内容作为第一个查询的 where 子句,因为这很可能是造成所有麻烦的原因:

其中 id.Contains(accountsitemapping.ACCOUNTID)

关于asp.net - SQL 语句的某些部分嵌套得太深。重写查询或将其分解为更小的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14175675/

相关文章:

c# - [目录未找到异常 : Could not find a part of the path

c# - 如何保存枚举mvc的字符串值

c# - 在 Entity Framework 中查找变更集

html - 如何将 HTML CSS 设计应用于 TreeView 控件

.net - 使用 ServiceHost 在单元测试中托管 WCF 服务

javascript - 将 MDL 与 ASP.NET Blazor 结合使用

具有 Web API 身份验证的 AngularJS

c# - NHibernate 问题与每个请求的 session MVC 应用程序

c# - 如何在 Lambda LINQ 表达式中创建 LEFT JOIN

c# - Entity Framework 中的字符串匹配问题。适用于字符串文字但不适用于字符串变量