0

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

3
  • Check out this question stackoverflow.com/questions/5640259/… Commented Mar 30, 2021 at 22:25
  • What do you mean by a SplitRequest which the logged in user has not attended to? Is this every splitRequest where.ApproverUserId != loggedInUserId ? Commented Mar 30, 2021 at 22:26
  • From eyeballing the code, it looks like your Linq code is only selecting SplitRequests that do have a SplitRequestApproval where the user is someone other than the current user. So SplitRequests that don't have a SplitRequestApproval at all won't be included. Commented Mar 30, 2021 at 22:27

1 Answer 1

1

Your Linq code is selecting SplitRequestApprovals where the user is not the current user, then navigating to the SplitRequest.

This means that SplitRequests without any SplitRequestApprovals at all won't be selected, which I think is where your problem is. Also, SplitRequests with approvals from multiple users will be selected multiple times.

What you want to do is select SplitRequests that don't have any SplitRequestApprovals by the current user. Something like this:

var request = await _context.SplitRequest
    .Where(c => !c.SplitRequestApprovals.Any(c => c.ApproverUserId == loggedInUserId))
    .ToListAsync()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.