var transferSummaries = await dbContext.MaterialTransactionTrackings .Where(ts =>…
- User
var transferSummaries = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == id) .OrderBy(ts=> ts.RouteDetailNavigation.Sequence) .Select(mts => new RoutesTransferDto{ SubLocation = mts.RouteDetailNavigation.RouteLocationNavigation.LocationName, MainLocation = mts.RouteDetailNavigation.RouteLocationNavigation.MainLocation.LocationName, Approver = mts.Approver, Status = mts.Approver == null ? "On Progression" : "Approved", ApproverList = string.Join(", ", _dbContext.LocationAuthorities .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation) .Select(la => la.Username)) //PendingApprove = }) .ToListAsync();
let say say we have 5 data, if the last approver is null, pending become "OK"
- Assistant
To set the value of the "PendingApprove" property based on the last approver being null, you can modify the query as follows:
var transferSummaries = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == id) .OrderBy(ts => ts.RouteDetailNavigation.Sequence) .Select(mts => new RoutesTransferDto { SubLocation = mts.RouteDetailNavigation.RouteLocationNavigation.LocationName, MainLocation = mts.RouteDetailNavigation.RouteLocationNavigation.MainLocation.LocationName, Approver = mts.Approver, Status = mts.Approver == null ? "On Progression" : "Approved", ApproverList = string.Join(", ", _dbContext.LocationAuthorities .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation) .Select(la => la.Username)), PendingApprove = mts.Approver == null && mts == _dbContext.MaterialTransactionTrackings.LastOrDefault() ? "OK" : null }) .ToListAsync();In the code above, I added the condition
mts.Approver == null && mts == _dbContext.MaterialTransactionTrackings.LastOrDefault()to check if the current record is the last one and the Approver is null. If it satisfies the condition, the value of "PendingApprove" will be set to "OK", otherwise it will be set to null. - User
- Assistant
The error message suggests that there is a problem with the LINQ query and its execution. It seems that the issue might be related to the
LastOrDefault()method used in the query.To fix the issue, you can try retrieving the MaterialTransactionTrackings separately and then perform the check for the last approver outside the LINQ query. Here's an updated version of the code:
var materialTransactionTrackings = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == id) .OrderBy(ts => ts.RouteDetailNavigation.Sequence) .ToListAsync(); var lastApproverIsNull = materialTransactionTrackings.LastOrDefault()?.Approver == null; var transferSummaries = materialTransactionTrackings .Select(mts => new RoutesTransferDto { SubLocation = mts.RouteDetailNavigation.RouteLocationNavigation.LocationName, MainLocation = mts.RouteDetailNavigation.RouteLocationNavigation.MainLocation.LocationName, Approver = mts.Approver, Status = mts.Approver == null ? "On Progression" : "Approved", ApproverList = string.Join(", ", _dbContext.LocationAuthorities .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation) .Select(la => la.Username)), PendingApprove = lastApproverIsNull ? "OK" : null }) .ToList(); - User
at API.Controllers.TransactionStatusController.<>c__DisplayClass6_0.b__0(MaterialTransactionTracking mts) in C:\Users\AdityaYulfan\Documents\IFBT Projects\Itrack_V2\API\Controllers\TransactionStatusController.cs:line 158\r\n at System.Linq.Enumerable.SelectListIterator1 actionResultValueTask)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)\r\n at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\r\n at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n at API.Middleware.ExceptionMiddleware.InvokeAsync(HttpContext context) in C:\Users\AdityaYulfan\Documents\IFBT Projects\Itrack_V2\API\Middleware\ExceptionMiddleware.cs:line 23
- Assistant
The error message suggests that there is an issue with the LINQ query when trying to retrieve the
Approverproperty from thematerialTransactionTrackingscollection.To troubleshoot the issue, please ensure that the
Approverproperty exists in the entityMaterialTransactionTrackingand is accessible. Check the data type and make sure it matches the property's type.Additionally, you can try modifying the LINQ query to include
ToList()after theWhereclause to ensure that the query execution is happening at that point and not later in the code. Here's an updated version of the code: - User
still error, this the MaterialTransactionTracking public partial class MaterialTransactionTracking { public int TrackingId { get; set; }
public int MaterialTransferSummary { get; set; } public int RouteDetail { get; set; } public string? Approver { get; set; } public DateTime? CreatedDate { get; set; } public DateTime? ModifyDate { get; set; } public virtual MaterialTransferSummary MaterialTransferSummaryNavigation { get; set; } = null!; public virtual RouteDetail RouteDetailNavigation { get; set; } = null!;}
- Assistant
Thank you for providing the
MaterialTransactionTrackingclass definition. Based on the code you shared, theApproverproperty should be of typestring?.The error you're encountering might be related to the
Approverproperty not being nullable in the database or entity configuration. To resolve the issue, you can update your entity configuration to make theApproverproperty nullable.If you're using Fluent API for configuring your entities, you can add the following code in your
DbContext'sOnModelCreatingmethod:protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<MaterialTransactionTracking>() .Property(mtt => mtt.Approver) .IsRequired(false); // make Approver nullable // ... other entity configurations }If you're using data annotations for entity configuration, you can update the
Approverproperty with the[Required]attribute: - User
what does this means? materialTransactionTrackings.LastOrDefault()?.Approver == null;
- Assistant
The expression
materialTransactionTrackings.LastOrDefault()?.Approveris checking if theApproverproperty of the last element in thematerialTransactionTrackingslist is null. Here's a breakdown of the expression:-
materialTransactionTrackings.LastOrDefault(): Gets the last element from thematerialTransactionTrackingslist, ornullif the list is empty. If there are elements in the list, it returns the last element based on the order defined in theOrderByclause. -
?.: This is the null conditional operator. It's used to safely access theApproverproperty of the last element even if the last element is null. If the last element is not null, it returns the value of theApproverproperty; otherwise, it returns null. -
== null: Compares the result of the expressionmaterialTransactionTrackings.LastOrDefault()?.Approverwithnullto check if theApproverproperty is null.
-
- User
Ok, update if get last update, we retrieve TrackingId
- Assistant
To retrieve the
TrackingIdof the last element in thematerialTransactionTrackingslist, you can modify the code as follows:var lastTrackingId = materialTransactionTrackings.LastOrDefault()?.TrackingId;In this code,
materialTransactionTrackings.LastOrDefault()?.TrackingIdretrieves theTrackingIdproperty of the last element in thematerialTransactionTrackingslist. If the list is empty or the last element isnull, it will returnnull. Otherwise, it will return the value of theTrackingIdproperty.You can then use the
lastTrackingIdvariable as needed in your code.