Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ-如果表不包含任何剩余数据,外部联接查询将返回异常_C#_Linq - Fatal编程技术网

C# LINQ-如果表不包含任何剩余数据,外部联接查询将返回异常

C# LINQ-如果表不包含任何剩余数据,外部联接查询将返回异常,c#,linq,C#,Linq,我有一个查询,它在linq中执行与子查询的左连接。但子查询没有任何值。在这种情况下,linq抛出一个异常。这是我的密码 IEnumerable<MemberDetailsViewModel> query = await (from account in _context.Account join kym in _context.KYMMaster on account

我有一个查询,它在linq中执行与子查询的左连接。但子查询没有任何值。在这种情况下,linq抛出一个异常。这是我的密码

IEnumerable<MemberDetailsViewModel> query = await (from account in _context.Account
                                                           join kym in _context.KYMMaster on account.ClientId equals kym.Id
                                                           join address in _context.KYMAddress on kym.Id equals address.MasterId
                                                           join branch in _context.Branches on kym.BranchId equals branch.Branch_Id
                                                           join deposit in (
                                                                //this subquery doesnot have any value
                                                                from d in _context.Deposit
                                                                where d.AccountNo == accountNo
                                                                group d by d.AccountNo into grp
                                                                select new
                                                                {
                                                                    AccountNo = grp.Key,
                                                                    Balance = grp.Sum(x => x.Amount)
                                                                }
                                                           ) on account.AccountNo equals deposit.AccountNo into temp
                                                           from deposit in temp.DefaultIfEmpty()
                                                           where account.AccountNo == accountNo
                                                           select new MemberDetailsViewModel
                                                           {
                                                               ClientId = kym.ClientId,
                                                               ClientName = kym.ClientName,
                                                               Mobile = kym.MobileNo,
                                                               Address = AddressHelper.GetAddress(address),
                                                               Branch = branch.Branch_Name,
                                                               AvailableBalance = deposit == null ? 0.00M : deposit.Balance
                                                               //AvailableBalance = temp == null ? 0.00M : temp.
                                                           })
                       .AsNoTracking()
                       .ToListAsync();
正如代码子查询中提到的,在我执行左连接的地方,子查询没有任何值。我该如何解决这个问题

这里是实际的例外

System.InvalidOperationException:可为null的对象必须具有值。 在lambda_method108Closure、QueryContext、DbDataReader、, 结果上下文,SingleQueryResultCoordinator位于 Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.AsyncEnumerator.MoveNextAsync位于Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListSync[TSource]IQueryable1 源,CancellationToken CancellationToken位于 Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListSync[TSource]IQueryable1源,CancellationToken CancellationToken位于D:\Project\Inventory\netcoreapp\Repository\Implementation\TechnoSys.Coop.Repository\Depository.cs中的TechnoSys.Coop.Repository.DepositoryRepository.GetMemberDetailsByAccountAsyncString accountNo中的TechnoSys.Coop.Service.DepositoryService.GetMemberDetailsByAccountAsyncString accountNo第53行D:\Project\Inventory\netcoreapp\Service\Implementation\TechnoSys.Coop.Service\DepostService.cs:TechnoSys.Coop.Controller.DepostController.GetAccountForDepositString accountNo中的第75行,位于D:\Project\Inventory\netcoreapp\Controller\TechnoSys.Coop.Controller\DepostController.cs:lambda\u方法101Closure中的第23行,对象位于Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.ExecuteActionResultTypeMapper映射器,ObjectMethodExecutor,对象控制器,对象[]参数位于Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g|Uu等待| 12_0 ControllerActionInvoker调用器,ValueTask1操作结果ValueTask位于 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g|u waiting | 10_0ControllerActionInvoker 调用程序、任务lastTask、状态next、作用域、对象状态、布尔值 完成于 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.RethrowActionExecutedContextSealed 上下文 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.NextState& 接下来,范围和范围、对象和状态、布尔值和在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g|u waiting | 13_0ControllerActionInvoker 调用程序、任务lastTask、状态next、作用域、对象状态、布尔值 完成于 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g|u waiting|19_0ResourceInvoker 调用程序、任务lastTask、状态next、作用域、对象状态、布尔值 完成于 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g|u Logged | 17|u 1ResourceInvoker 调用程序 Microsoft.AspNetCore.Routing.EndpointMiddleware.g|u waitRequestTask | 6_0Endpoint 端点、任务请求任务、ILogger记录器位于 Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleSyncRequestDelegate 接下来是HttpContext上下文,AuthorizationPolicy策略, PolicyAuthorizationResult AuthorizationResult位于 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.InvokeHttpContext 上下文 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.InvokeHttpContext 上下文 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.InvokeHttpContext 上下文


简化了LINQ转换器的查询,应该可以正常工作

var存款查询= 来自d in_.存款 其中d.AccountNo==AccountNo d组按d.AccountNo分为grp 选择新的 { AccountNo=grp.Key, 余额=grp.Sumx=>x.金额 }; 变量查询= 来自_context.account中的帐户 在account.ClientId等于kym.Id的_context.KYMMaster中加入kym 在kym.Id上的_context.KYMAddress中加入地址等于address.MasterId 在kym.branchhid上的_context.branchs中联接分支等于branch.branch\u Id 将存款加入account.AccountNo上的存款查询等于deposit.AccountNo加入临时帐户 从temp.DefaultIfEmpty中的存款 其中account.AccountNo==AccountNo 选择新成员详细信息查看模型 { ClientId=kym.ClientId, ClientName=kym.ClientName, Mobile=kym.MobileNo, Address=AddressHelper.GetAddressaddress, Branch=Branch.Branch\u名称, 可用余额=十进制?存款余额??0.00M }; var result=等待查询 .阿斯诺特拉克 .ToListAsync;
首先,您可以将看到的执行选项添加到问题中…发布实际的完整异常文本,而不仅仅是消息。由于表是空的,因此不会抛出此项。LINQ查询被转换为SQL,而SQL并不关心空值。如果没有行,则没有行。然而,整个查询是一个非常强烈的警告,表明设计完全是错误的。LINQ不是SQL的替代品。是的 ORM的工作是从关系中生成连接。如果需要在LINQ查询中使用5个连接,则可能是模型错误,也可能是不应该使用LINQ