System.StackOverflowException

621 阅读1分钟

**System.StackOverflowException

HResult=0x800703E9

Message=Exception_WasThrown**

![40}%YVJN1G`DV(]AL)G%5Q6.png](p9-juejin.byteimg.com/tos-cn-i-k3…)

原因:List(orderProcessIds)的数据量越大,导致拼接的语句越长.....

WJ4WID{7V0L6AMVLNJ}X@TP.png

解决方案: 使用Linq写法

            var items = (from dispatch in _dispatchListRepository.GetAll()
                         join processInfo in _orderProcessRepository.GetAll() on dispatch.OrderProcessId equals processInfo.Id into processItems
                         from process in processItems.DefaultIfEmpty()
                         join materialInfo in _materialRepository.GetAll() on process.MaterialId equals materialInfo.Id into materialItmes
                         from material in materialItmes.DefaultIfEmpty()
                         where (string.IsNullOrEmpty(productionOrderCode) || process.ProductionOrder.ProductionOrderNo.Contains(productionOrderCode)) &&
                         (string.IsNullOrEmpty(materialCode) || material.MaterialCode.Contains(materialCode)) &&
                         (string.IsNullOrEmpty(materialName) || material.MaterialName.Contains(materialName)) &&
                         (string.IsNullOrEmpty(materialSpecification) || material.MaterialSpecification.Contains(materialSpecification)) &&
                         (string.IsNullOrEmpty(materialModel) || material.MaterialModel.Contains(materialModel)) &&
                         (string.IsNullOrEmpty(workingProcedureName) || process.WorkingProcedure.WorkingProcedureName.Contains(workingProcedureName)) &&
                         string.IsNullOrEmpty(dispatch.ProductionAssociatedNumber) &&
                         newState.Contains(process.ProductionOrder.ProductionOrderState)
                         orderby dispatch.CreationTime descending, process.SequenceNumber ascending
                         select new
                         {
                             dispatch,
                             process,
                             ProductionOrderNo = process == null ? null : process.ProductionOrder.ProductionOrderNo,
                             MaterialCode = material == null ? null : material.MaterialCode,
                             MaterialName = material == null ? null : material.MaterialName,
                             MaterialSpecification = material == null ? null : material.MaterialSpecification,
                             MaterialModel = material == null ? null : material.MaterialModel,
                             MaterialNameAbbreviation = material == null ? null : material.MaterialNameAbbreviation,
                             WorkingProcedureId = process == null ? 0 : process.WorkingProcedure.Id,
                             WorkingProcedureCode = process == null ? null : process.WorkingProcedure.WorkingProcedureCode,
                             WorkingProcedureName = process == null ? null : process.WorkingProcedure.WorkingProcedureName,
                         });

            var totalCount = items.Count();
            var list = items.Skip((page - 1) * rows).Take(rows).ToList();

            var resutList = list.Select(a => a.dispatch).Distinct().MapTo<List<DispatchListDto>>();

            var operatorIds = resutList.Where(x => x.OperatorId.HasValue).Select(x => x.OperatorId).Distinct().ToList();
            var operatorList = await AsyncQueryableExecuter.ToListAsync(_userRepository.GetAll().Where(x => operatorIds.Contains(x.Id)));
            var equipmentIds = resutList.Where(x => x.EquipmentId.HasValue).Select(x => x.EquipmentId).Distinct().ToList();
            var equipmentList = await AsyncQueryableExecuter.ToListAsync(_equipmentListRepository.GetAll().Where(x => equipmentIds.Contains(x.Id)));
            resutList.ForEach(a =>
            {
                var item = list.FirstOrDefault(x => x.dispatch.Id == a.Id);
                a.ProductionOrderNo = item?.ProductionOrderNo;
                a.WorkingProcedureId = item?.WorkingProcedureId ?? 0;
                a.WorkingProcedureCode = item?.WorkingProcedureCode;
                a.WorkingProcedureName = item?.WorkingProcedureName;
                a.SerialNumber = item?.process?.SequenceNumber ?? 0;
                a.CompletionNumber = item?.process?.CompletionNumber ?? 0;
                a.UnqualifiedQuantitys = item?.process?.UnqualifiedQuantitys ?? 0;
                a.ScrapNumbers = item?.process?.ScrapNumbers ?? 0;
                a.PlanStartTime = item?.process?.PlanStartTime;
                a.PlanEndTime = item?.process?.PlanEndTime;
                a.MaterialCode = item?.MaterialCode;
                a.MaterialName = item?.MaterialName;
                a.MaterialSpecification = item?.MaterialSpecification;
                a.MaterialModel = item?.MaterialModel;
                a.MaterialNameAbbreviation = item?.MaterialNameAbbreviation;
                var operators = operatorList.FirstOrDefault(b => b.Id == a.OperatorId);
                if (operators != null)
                {
                    a.OperatorId = operators.Id;
                    a.OperatorNameStr = operators.Name;
                }
                var equipment = equipmentList.FirstOrDefault(b => b.Id == a.EquipmentId);
                a.EquipmentNameStr = equipment?.EquipmentName ?? "";
            });
            return new PagedResultDto<DispatchListDto>(totalCount, resutList);