Linq Verbosity Problem

Hello Linq Legends.

There must be a way to refactor a method out of the following code as a more elegant and reusable method possibly using Action delegates in linq. (?)

Basically, RmdSessionRequest parameter is an entity object and the IRmdSessionRequestDetails is a dto with nullable int ids which correspond to linked entities in the entity. If either the entity object is null and the dto id isn't, or they are both not null but not equal, i need to update the entity with the dto value.

public void UpdateTriageRequest(RmdSessionRequest request, IRmdSessionRequestDetails requestDetails)
        {
            if ((request.Diagnostician == null && requestDetails.DiagnosticianId.HasValue ) ||
                (request.Diagnostician != null && requestDetails.DiagnosticianId.HasValue && !requestDetails.DiagnosticianId.Value.Equals(request.Diagnostician.Id)))
            {
                var diagnostician = _userRepository.Get(requestDetails.DiagnosticianId.Value);
                request.Diagnostician = diagnostician;
            }

            if ((request.SpecimenIdentification == null && requestDetails.SpecimenIdentificationId.HasValue) ||
                (request.SpecimenIdentification != null && requestDetails.SpecimenIdentificationId.HasValue && !requestDetails.SpecimenIdentificationId.Equals(request.SpecimenIdentification.Id)))
            {
                var specimenIdentification = _specimenIdentificationRepository.Get(requestDetails.SpecimenIdentificationId.Value);
                request.SpecimenIdentification = specimenIdentification;
            }

            if ((request.RmdNode == null && requestDetails.RmdNodeId.HasValue) ||
                (request.RmdNode != null && requestDetails.RmdNodeId.HasValue && !requestDetails.RmdNodeId.Value.Equals(request.RmdNode.Id)))
            {
                var rmdNode = _rmdNodeRepository.Get(requestDetails.RmdNodeId.Value);
                request.RmdNode = rmdNode;
            }
        }

Open in new window


This is a snippet of a much larger method so if  someone can help me craft the above kind of if statement into more beautiful and readable code, I'd be very grateful!!!

Cheers.
crittoAsked:
Who is Participating?
 
MikeTooleCommented:
Test the HasValue once at the beginning - if it's false nothing after the && will be executed
The || is a short-circuit, if the null comparison returns true, the right-hand isn't executed, so there's no need for the !null test.
The result of the Get can be assigned to the request in the same line.

Not sure without testing whether you can get away withou the check for null, but this would be worth a try:
            if (requestDetails.DiagnosticianId.HasValue &&  (!requestDetails.DiagnosticianId.Value.Equals(request.Diagnostician.Id)))



if (requestDetails.DiagnosticianId.HasValue && 
               ((request.Diagnostician == null) || (!requestDetails.DiagnosticianId.Value.Equals(request.Diagnostician.Id))))
            {
                request.Diagnostician = _userRepository.Get(requestDetails.DiagnosticianId.Value);
            }

Open in new window

0
 
crittoAuthor Commented:
Apologies for not closing this sooner.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.