linq一对多错误记录

错误日志

ERROR 2020-07-27 09:14:41,870 [4 ] EvaluationSystem.CustomeExceptionFilter - variable 'list' of type 'System.Collections.Generic.IEnumerable`1[EvaluationSystem.EntityDesign.SocietyScore]' referenced from scope '', but it is not defined
System.InvalidOperationException: variable 'list' of type 'System.Collections.Generic.IEnumerable`1[EvaluationSystem.EntityDesign.SocietyScore]' referenced from scope '', but it is not defined
at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitNew(NewExpression node)
at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
at System.Linq.Expressions.LambdaExpression.Compile()
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQueryExpression(ShapedQueryExpression shapedQueryExpression)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at EvaluationSystem.SocietyTeam.SocietyTeamAppService.SheTuanPaiMing(SheTuanPaiMingInput input) in E:Workspaces华发工作空间路北区智慧校园平台aspnet-coresrcEvaluationSystem.ApplicationSocietyTeamSocietyTeamAppService.cs:line 119
at lambda_method(Closure , Object )
at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)

代码

  /// <summary>
        /// 社团排名
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<SheTuanPaiMingList>> SheTuanPaiMing(SheTuanPaiMingInput input)
        {
            var school = _schoolInformationRepository.GetAll();
            if (input.CantonId.HasValue)
            {
                school = school.Where(x => x.CanTonId == input.CantonId);
            }

            if (input.SchoolId.HasValue)
            {
                school = school.Where(x => x.Id == input.SchoolId);
            }

            var query = from societyTeam in _societyTeamRepository.GetAll()
                        join societyScore in _societyScoreRepository.GetAll() on societyTeam.Id equals societyScore.SocietyId into list
                        from d in list.DefaultIfEmpty()
                        join schoolInformation in school on societyTeam.SchoolId equals schoolInformation.Id
                        join schoolTerm in _schoolTermRepository.GetAll() on societyTeam.SchoolTermId equals schoolTerm.Id
                        where (!input.TermId.HasValue || societyTeam.SchoolTermId == input.TermId)

                        select new { societyTeam, schoolInformation, schoolTerm, list };
            var totalCount = await query.CountAsync();
            var items = await query.OrderBy(x=>x.societyTeam.SocietyTeamName).PageBy(input).ToListAsync();


            return new PagedResultDto<SheTuanPaiMingList>(totalCount, items.Select(item =>
            {
                var dto = new SheTuanPaiMingList();

                dto.SchoolTermId = item.schoolTerm.Id;
                dto.SchoolTermName = item.schoolTerm.TermName;
                dto.SchoolId = item.schoolInformation.Id;
                dto.SchoolName = item.schoolInformation.SchoolName;
                dto.SocietyTeamName = item.societyTeam.SocietyTeamName;
                dto.SocietyTeamId = item.societyTeam.Id;
                dto.TotailScore = item.list.Select(x => x.Score).Sum();
                dto.Score.AddRange(item.list.Select(x =>
                {
                    var d = new ScoreTemp();
                    d.Score = x.Score;
                    d.IndexName = x.IndexName;
                    return d;
                }).ToList());
                dto.CreationTime = item.societyTeam.CreationTime;
                dto.Memo = item.societyTeam.Mark;
                return dto;
            }).ToList());
        }

 解决方案:


原文地址:https://www.cnblogs.com/wangyinlon/p/13383646.html