kommissar
kommissar
CC#
Created by kommissar on 2/6/2023 in #help
❔ How do I make part of a Linq query optional?
I have a query like this:
var newInfo = from ops in _radanContext.RouteCardOps
join rc in _radanContext.RouteCards on ops.RouteNo equals rc.RouteNo
join con in _radanContext.Contracts on rc.ContractNo equals con.ContractNo
join parts in _radanContext.Parts on new { rc.PartNum, rc.Issue } equals new { parts.PartNum, parts.Issue }
select new CompletedJobResult
{
RouteNo = rc.RouteNo,
Seq = ops.Seq,
OpCode = ops.OpCode,
Issue = rc.Issue,
Description = ops.RouteDesc,
BatchQty = rc.BatchQty,
Qtycomplete = ops.QtyComplete,
ContractNo = con.ContractNo,
CustCode = con.CustCode,
PartNum = parts.Description,
BatchNo = rc.BatchNo
};
var newInfo = from ops in _radanContext.RouteCardOps
join rc in _radanContext.RouteCards on ops.RouteNo equals rc.RouteNo
join con in _radanContext.Contracts on rc.ContractNo equals con.ContractNo
join parts in _radanContext.Parts on new { rc.PartNum, rc.Issue } equals new { parts.PartNum, parts.Issue }
select new CompletedJobResult
{
RouteNo = rc.RouteNo,
Seq = ops.Seq,
OpCode = ops.OpCode,
Issue = rc.Issue,
Description = ops.RouteDesc,
BatchQty = rc.BatchQty,
Qtycomplete = ops.QtyComplete,
ContractNo = con.ContractNo,
CustCode = con.CustCode,
PartNum = parts.Description,
BatchNo = rc.BatchNo
};
My problem is that the contracts should be optional - as sometimes rc.ContractNo can be null. How can I achieve this?
5 replies
CC#
Created by kommissar on 2/6/2023 in #help
Entity Framework - DbUpdateConcurrencyException due to a trigger on the table
I've been dealing with a problem for a very long time, and I've got no idea how to fix it. I'm a bit of a noob at Entity Framework, so any help appreciated.
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
After a very long period of trial and error, I found that the table has a trigger on it, which must update some entities. The result is the above exception. I have no idea how to fix this, my code is extremely simple.
public async Task<Response<bool>> Handle(UpdateLastOperationCommand request, CancellationToken cancellationToken)
{
var routeCard = await _myDbContext.RouteCards.FirstOrDefaultAsync(n => n.RouteNo == request.RouteNo);
var routeOperations =
await _myDbContext.RouteCardOps.Where(n => n.RouteNo == request.RouteNo).ToListAsync();
var ordered = routeOperations.OrderBy(n => n.Seq);
var lastOp = ordered.LastOrDefault();

if (lastOp == null)
{
return new Response<bool>() { Succeeded = true, Data = false, Message = "No operation to update" };
}

routeCard.QtyFinished = lastOp.QtyComplete;
if (routeCard.QtyFinished >= routeCard.BatchQty)
{
Log.Information($"RouteCard should be marked as complete");
routeCard.RouteStatus = "CMP";
routeCard.StatusDate = DateTime.Now;
routeCard.CompletedDate = DateTime.Now;
routeCard.SignOff = lastOp.SignOff;
routeCard.SignOffOn = lastOp.SignOffOn;
}

await _myDbContext.SaveChangesAsync();
Log.Information($"Successfully updated route card {request.RouteNo}");

return new Response<bool>() { Succeeded = true, Data = true };
}
public async Task<Response<bool>> Handle(UpdateLastOperationCommand request, CancellationToken cancellationToken)
{
var routeCard = await _myDbContext.RouteCards.FirstOrDefaultAsync(n => n.RouteNo == request.RouteNo);
var routeOperations =
await _myDbContext.RouteCardOps.Where(n => n.RouteNo == request.RouteNo).ToListAsync();
var ordered = routeOperations.OrderBy(n => n.Seq);
var lastOp = ordered.LastOrDefault();

if (lastOp == null)
{
return new Response<bool>() { Succeeded = true, Data = false, Message = "No operation to update" };
}

routeCard.QtyFinished = lastOp.QtyComplete;
if (routeCard.QtyFinished >= routeCard.BatchQty)
{
Log.Information($"RouteCard should be marked as complete");
routeCard.RouteStatus = "CMP";
routeCard.StatusDate = DateTime.Now;
routeCard.CompletedDate = DateTime.Now;
routeCard.SignOff = lastOp.SignOff;
routeCard.SignOffOn = lastOp.SignOffOn;
}

await _myDbContext.SaveChangesAsync();
Log.Information($"Successfully updated route card {request.RouteNo}");

return new Response<bool>() { Succeeded = true, Data = true };
}
Any help would be appreciated.
15 replies
CC#
Created by kommissar on 2/6/2023 in #help
Serialising and deserialising json derived file
Hello. I have a json derived file (but not quite standard json) that I'd like to serialise and deserialise. Any ideas how to do it easily without writing my own parser? Honestly I wouldn't know where to start.
24 replies