2

I have the following code where I get error when loading Peers:

Value cannot be null. Parameter name: source

I am using FirstOrDefault and DefaultIfEmpty methods, and inside the select statement I am also checking if the object is empty m => m == null ?. But, I cannot avoid the error. Any ideas?

 ReviewRoundDTO_student results = _context.ReviewRounds
                .Include(rr => rr.ReviewTasks).ThenInclude(rt => rt.ReviewTaskStatuses)
                .Include(rr => rr.Submissions).ThenInclude(s => s.PeerGroup.PeerGroupMemberships).ThenInclude(m => m.User)
                .Include(rr => rr.Rubric)
                .Where(rr => rr.Id == reviewRoundId)
                .Select(rr => new ReviewRoundDTO_student
                {
                    Id = rr.Id,
                    SubmissionId = rr.Submissions.FirstOrDefault(s => s.StudentId == currentUser.Id).Id,
                    Peers = rr.Submissions.FirstOrDefault(s => s.StudentId == currentUser.Id)
                                .PeerGroup.PeerGroupMemberships.DefaultIfEmpty()
                                .Select(m => m == null ? new ApplicationUserDto { } : new ApplicationUserDto
                                {
                                    //FullName = m.User.FullName,
                                    //Id = new Guid(m.UserId)
                                }),

                    }).FirstOrDefault();
4
  • The entity ReviewRounds' Submissions property needs to be initialized to a new SomeCollection<T>() in constructor. Commented May 23, 2019 at 16:15
  • @MatJ thanks for the response. Could you please elaborate a bit more? Commented May 23, 2019 at 16:16
  • 1
    @EdPlunkett This is LINQ to Entities projection query - it has to be translated to SQL, so there are no "object"s involved at all - entities are used just as metadata. Commented May 23, 2019 at 16:26
  • @IvanStoev Well, I think there's a good chance something might be null in there somewhere. Commented May 23, 2019 at 16:27

1 Answer 1

3

Try avoiding FirstOrDefault().Something construct - expression trees do not support ?. operator which you'd normally use in similar LINQ to Objects query, and EF Core currently has issues translating it correctly - if you look at the exception stack trace, most likely the exception is coming deeply from EF Core infrastructure with no user code involved.

I would recommend rewriting the LINQ query w/o such constructs, for instance something like this:

var results = _context.ReviewRounds
    .Where(rr => rr.Id == reviewRoundId)
    .Select(rr => new ReviewRoundDTO_student
    {
        Id = rr.Id,
        SubmissionId = rr.Submissions
           .Where(s => s.StudentId == currentUser.Id)
           .Select(s => s.Id)
           .FirstOrDefault(),
        Peers = rr.Submissions
            .Where(s => s.StudentId == currentUser.Id)
            .Take(1)
            .SelectMany(s => s.PeerGroup.PeerGroupMemberships)
            .Select(m => new ApplicationUserDto
            {
                FullName = m.User.FullName,
                Id = m.UserId
            })
            .ToList(),
   })
   .FirstOrDefault();

Note that Include / ThenInclude are not needed in projection queries, because they are ignored.

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.