Please consider the following scenario,
I have a two tables SplitRequest and SplitRequestApproval.
SplitRequest has a collection of SplitRequestApproval while SplitRequestApproval has one of SplitRequest
What the above means is that there is a one to many relationship between SplitRequest and SplitRequestApproval
SplitRequestApproval has ApproverUserId SplitRequestId DateApproved etc... as column. while SplitRequest has the actual request details.
The question then is I want a linq query that will get List<SplitRequest> that a logged in user (Approver) has not attended too.
So if user A with ID 123 Logs in, and navigates to the SplitRequest View, The view should only show request he has not attended to yet. therefore if he approves a SplitRequest wiht ID 12 that item should not be part of the request visible to him anymore. SplitRequestApproval gets a new row with SplitRequestId as 12 and Userid 123
How can this be achieved with linq.
var request = await _context.SplitRequestApproval
.Include(c => c.SplitRequest)
.Where(c => c.ApproverUserId != loggedInUserId)
.Select(c => c.SplitRequest)
.ToListAsync();
this is returning 0 items. whereas there are SplitRequest the loggedin user has not attended to
A solution I got but i considered it a long route
//gets the list of splitRequests
var splitRequests = await _context.SplitRequest.ToListAsync();
//loops through each request
foreach (var item in splitRequests)
{
//check if the request has been approved by the user
var IsExistApprovalForLoggedInUser = await _context.SplitRequestApproval.AnyAsync(c => c.SplitRequestId == item.SplitRequestId && c.ApproverUserId == loggedInUserId);
if (IsExistApprovalForLoggedInUser)
{
//if yes remove from list
splitRequests.Remove(item);
}
}
I think there should be a linq query that will give me same result and faster